Skip to content

Commit

Permalink
Merge pull request apache#805 from mercyblitz:2.5.7
Browse files Browse the repository at this point in the history
Refactor annotation config
  • Loading branch information
mercyblitz authored and chickenlj committed Nov 3, 2017
1 parent 6011b36 commit a2c442f
Show file tree
Hide file tree
Showing 79 changed files with 1,730 additions and 60 deletions.
2 changes: 1 addition & 1 deletion dubbo-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-parent</artifactId>
<version>2.5.6</version>
<version>2.5.7</version>
</parent>
<artifactId>dubbo-admin</artifactId>
<packaging>war</packaging>
Expand Down
2 changes: 1 addition & 1 deletion dubbo-cluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-parent</artifactId>
<version>2.5.6</version>
<version>2.5.7</version>
</parent>
<artifactId>dubbo-cluster</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion dubbo-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-parent</artifactId>
<version>2.5.6</version>
<version>2.5.7</version>
</parent>
<artifactId>dubbo-common</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion dubbo-config/dubbo-config-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-config</artifactId>
<version>2.5.6</version>
<version>2.5.7</version>
</parent>
<artifactId>dubbo-config-api</artifactId>
<packaging>jar</packaging>
Expand Down
9 changes: 8 additions & 1 deletion dubbo-config/dubbo-config-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-config</artifactId>
<version>2.5.6</version>
<version>2.5.7</version>
</parent>
<artifactId>dubbo-config-spring</artifactId>
<packaging>jar</packaging>
Expand Down Expand Up @@ -92,6 +92,13 @@
<artifactId>javax.el</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean

private static transient ApplicationContext SPRING_CONTEXT;

private final transient Service service;

private transient ApplicationContext applicationContext;

private transient String beanName;
Expand All @@ -61,10 +63,12 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean

public ServiceBean() {
super();
this.service = null;
}

public ServiceBean(Service service) {
super(service);
this.service = service;
}

public static ApplicationContext getSpringContext() {
Expand Down Expand Up @@ -100,6 +104,15 @@ public void setBeanName(String name) {
this.beanName = name;
}

/**
* Gets associated {@link Service}
*
* @return associated {@link Service}
*/
public Service getService() {
return service;
}

public void onApplicationEvent(ApplicationEvent event) {
if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
if (isDelay() && !isExported() && !isUnexported()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package com.alibaba.dubbo.config.spring.beans.factory.annotation;

import com.alibaba.dubbo.config.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

import java.lang.annotation.Annotation;
import java.util.List;

import static com.alibaba.dubbo.config.spring.util.BeanFactoryUtils.getBeans;
import static com.alibaba.dubbo.config.spring.util.BeanFactoryUtils.getOptionalBean;

/**
* Abstract Configurable {@link Annotation} Bean Builder
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @since 2.5.7
*/
abstract class AbstractAnnotationConfigBeanBuilder<A extends Annotation, B extends AbstractInterfaceConfig> {

protected final Log logger = LogFactory.getLog(getClass());

protected final A annotation;

protected final ApplicationContext applicationContext;

protected final ClassLoader classLoader;

protected Object bean;

protected Class<?> interfaceClass;

protected AbstractAnnotationConfigBeanBuilder(A annotation, ClassLoader classLoader,
ApplicationContext applicationContext) {
Assert.notNull(annotation, "The Annotation must not be null!");
Assert.notNull(classLoader, "The ClassLoader must not be null!");
Assert.notNull(applicationContext, "The ApplicationContext must not be null!");
this.annotation = annotation;
this.applicationContext = applicationContext;
this.classLoader = classLoader;

}

/**
* Build {@link B}
*
* @return non-null
* @throws Exception
*/
public final B build() throws Exception {

checkDependencies();

B bean = doBuild();

configureBean(bean);

if (logger.isInfoEnabled()) {
logger.info(bean + " has been built.");
}

return bean;

}

private void checkDependencies() {

}

/**
* Builds {@link B Bean}
*
* @return {@link B Bean}
*/
protected abstract B doBuild();


protected void configureBean(B bean) throws Exception {

preConfigureBean(annotation, bean);

configureRegistryConfigs(bean);

configureMonitorConfig(bean);

configureApplicationConfig(bean);

configureModuleConfig(bean);

postConfigureBean(annotation, bean);

}

protected abstract void preConfigureBean(A annotation, B bean) throws Exception;


private void configureRegistryConfigs(B bean) {

String[] registryConfigBeanIds = resolveRegistryConfigBeanNames(annotation);

List<RegistryConfig> registryConfigs = getBeans(applicationContext, registryConfigBeanIds, RegistryConfig.class);

bean.setRegistries(registryConfigs);

}

private void configureMonitorConfig(B bean) {

String monitorBeanName = resolveMonitorConfigBeanName(annotation);

MonitorConfig monitorConfig = getOptionalBean(applicationContext, monitorBeanName, MonitorConfig.class);

bean.setMonitor(monitorConfig);

}

private void configureApplicationConfig(B bean) {

String applicationConfigBeanName = resolveApplicationConfigBeanName(annotation);

ApplicationConfig applicationConfig =
getOptionalBean(applicationContext, applicationConfigBeanName, ApplicationConfig.class);

bean.setApplication(applicationConfig);

}

private void configureModuleConfig(B bean) {

String moduleConfigBeanName = resolveModuleConfigBeanName(annotation);

ModuleConfig moduleConfig =
getOptionalBean(applicationContext, moduleConfigBeanName, ModuleConfig.class);

bean.setModule(moduleConfig);

}

/**
* Resolves the bean name of {@link ModuleConfig}
*
* @param annotation {@link A}
* @return
*/
protected abstract String resolveModuleConfigBeanName(A annotation);

/**
* Resolves the bean name of {@link ApplicationConfig}
*
* @param annotation {@link A}
* @return
*/
protected abstract String resolveApplicationConfigBeanName(A annotation);


/**
* Resolves the bean ids of {@link com.alibaba.dubbo.config.RegistryConfig}
*
* @param annotation {@link A}
* @return non-empty array
*/
protected abstract String[] resolveRegistryConfigBeanNames(A annotation);

/**
* Resolves the bean name of {@link MonitorConfig}
*
* @param annotation {@link A}
* @return
*/
protected abstract String resolveMonitorConfigBeanName(A annotation);

/**
* Configures Bean
*
* @param annotation
* @param bean
*/
protected abstract void postConfigureBean(A annotation, B bean) throws Exception;


public <T extends AbstractAnnotationConfigBeanBuilder<A, B>> T bean(Object bean) {
this.bean = bean;
return (T) this;
}

public <T extends AbstractAnnotationConfigBeanBuilder<A, B>> T interfaceClass(Class<?> interfaceClass) {
this.interfaceClass = interfaceClass;
return (T) this;
}

}
Loading

0 comments on commit a2c442f

Please sign in to comment.