コード例 #1
1
 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
     throws BeansException {
   if (annotationPackage == null || annotationPackage.length() == 0) {
     return;
   }
   if (beanFactory instanceof BeanDefinitionRegistry) {
     try {
       // init scanner
       Class<?> scannerClass =
           ClassUtils.loadClass(
               "org.springframework.context.annotation.ClassPathBeanDefinitionScanner");
       Object scanner =
           scannerClass
               .getConstructor(new Class<?>[] {BeanDefinitionRegistry.class, boolean.class})
               .newInstance(new Object[] {(BeanDefinitionRegistry) beanFactory, true});
       // add filter
       Class<?> filterClass =
           ClassUtils.loadClass("org.springframework.core.type.filter.AnnotationTypeFilter");
       Object filter = filterClass.getConstructor(Class.class).newInstance(Service.class);
       Method addIncludeFilter =
           scannerClass.getMethod(
               "addIncludeFilter",
               ClassUtils.loadClass("org.springframework.core.type.filter.TypeFilter"));
       addIncludeFilter.invoke(scanner, filter);
       // scan packages
       String[] packages = Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);
       Method scan = scannerClass.getMethod("scan", new Class<?>[] {String[].class});
       scan.invoke(scanner, new Object[] {packages});
     } catch (Throwable e) {
       // spring 2.0
     }
   }
 }
コード例 #2
0
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!isMatchPackage(bean)) {
      return bean;
    }
    Class<?> beanClass = bean.getClass();
    int idxCglib =
        beanClass.getName().contains("$$EnhancerByCGLIB")
            ? beanClass.getName().indexOf("$$EnhancerByCGLIB")
            : beanClass.getName().indexOf("$$EnhancerBySpringCGLIB");
    if (idxCglib != -1) {
      try {
        beanClass = ClassUtils.loadClass(beanClass.getName().substring(0, idxCglib));
      } catch (ClassNotFoundException e) {
        throw new IllegalStateException(
            "Failed to export remote service class " + beanClass.getName(), e);
      }
    }
    Service service = beanClass.getAnnotation(Service.class);
    if (service != null) {
      Class serviceInterface = service.interfaceClass();
      if (void.class.equals(service.interfaceClass())) {
        serviceInterface = ServiceConfigUtils.getServiceInterface(beanClass);
      }
      if (serviceInterface == null) {
        serviceInterface = beanClass;
      }
      ProviderConfig<Object> providerConfig = new ProviderConfig<Object>(serviceInterface, bean);
      providerConfig.setService(bean);
      providerConfig.setUrl(service.url());
      providerConfig.setVersion(service.version());
      providerConfig.setSharedPool(service.useSharedPool());
      providerConfig.setActives(service.actives());

      ServerConfig serverConfig = new ServerConfig();
      serverConfig.setPort(getDefaultPort(service.port()));
      serverConfig.setGroup(service.group());
      serverConfig.setAutoSelectPort(service.autoSelectPort());
      providerConfig.setServerConfig(serverConfig);
      ServiceFactory.addService(providerConfig);
    }
    postProcessBeforeInitialization(bean, beanName);
    return bean;
  }
コード例 #3
0
 private Object refer(
     Reference reference, Class<?> referenceClass) { // method.getParameterTypes()[0]
   String interfaceName;
   if (!void.class.equals(reference.interfaceClass())) {
     interfaceName = reference.interfaceClass().getName();
   } else if (referenceClass.isInterface()) {
     interfaceName = referenceClass.getName();
   } else {
     throw new IllegalStateException(
         "The @Reference undefined interfaceClass or interfaceName, and the property type "
             + referenceClass.getName()
             + " is not a interface.");
   }
   String callbackClassName = reference.callback();
   InvocationCallback callback = null;
   if (StringUtils.isNotBlank(callbackClassName)) {
     Class<?> clazz;
     try {
       clazz = ClassUtils.loadClass(callbackClassName);
     } catch (ClassNotFoundException e) {
       throw new IllegalStateException(
           "The @Reference undefined callback "
               + callbackClassName
               + ", is not a ServiceCallback interface.");
     }
     if (!InvocationCallback.class.isAssignableFrom(clazz)) {
       throw new IllegalStateException(
           "The @Reference undefined callback "
               + callbackClassName
               + ", is not a ServiceCallback interface.");
     }
     try {
       callback = (InvocationCallback) clazz.newInstance();
     } catch (InstantiationException e) {
       throw new IllegalStateException(
           "The @Reference undefined callback "
               + callbackClassName
               + ", is not a ServiceCallback interface.");
     } catch (IllegalAccessException e) {
       throw new IllegalStateException(
           "The @Reference undefined callback "
               + callbackClassName
               + ", is not a ServiceCallback interface.");
     }
   }
   String key =
       reference.group()
           + "/"
           + reference.url()
           + "@"
           + interfaceName
           + ":"
           + reference.version()
           + ":"
           + reference.serialize()
           + ":"
           + reference.protocol()
           + ":"
           + reference.timeout()
           + ":"
           + reference.callType();
   InvokerConfig<?> invokerConfig = invokerConfigs.get(key);
   if (invokerConfig == null) {
     invokerConfig =
         new InvokerConfig(
             referenceClass,
             reference.url(),
             reference.timeout(),
             reference.callType(),
             reference.serialize(),
             callback,
             reference.group(),
             false,
             reference.loadbalance(),
             reference.cluster(),
             reference.retries(),
             reference.timeoutRetry(),
             reference.vip(),
             reference.version(),
             reference.protocol());
     invokerConfig.setSecret(reference.secret());
     invokerConfigs.putIfAbsent(key, invokerConfig);
     invokerConfig = invokerConfigs.get(key);
   }
   return ServiceFactory.getService(invokerConfig);
 }