Skip to content

Commit

Permalink
Polish #14 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Nov 1, 2017
1 parent ff59b2d commit 375cda0
Show file tree
Hide file tree
Showing 19 changed files with 1,164 additions and 193 deletions.
7 changes: 7 additions & 0 deletions dubbo-config/dubbo-config-spring/pom.xml
Original file line number Diff line number Diff line change
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
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
Expand Down Expand Up @@ -44,14 +42,20 @@
* @since 2.5.7
*/
public class ReferenceAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware, ApplicationContextAware {
implements MergedBeanDefinitionPostProcessor, PriorityOrdered, ApplicationContextAware, BeanClassLoaderAware,
DisposableBean {

private final Log logger = LogFactory.getLog(getClass());
/**
* The bean name of {@link ReferenceAnnotationBeanPostProcessor}
*/
public static String BEAN_NAME = "referenceAnnotationBeanPostProcessor";

private ConfigurableListableBeanFactory beanFactory;
private final Log logger = LogFactory.getLog(getClass());

private ApplicationContext applicationContext;

private ClassLoader classLoader;

private final ConcurrentMap<String, InjectionMetadata> injectionMetadataCache =
new ConcurrentHashMap<String, InjectionMetadata>(256);

Expand Down Expand Up @@ -207,16 +211,6 @@ private Reference findReferenceAnnotation(AccessibleObject accessibleObject) {

}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException(
getClass().getName() + " requires a ConfigurableListableBeanFactory");
}
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Expand All @@ -232,9 +226,32 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
return LOWEST_PRECEDENCE;
}

@Override
public void destroy() throws Exception {

for (ReferenceBean referenceBean : referenceBeansCache.values()) {
if (logger.isInfoEnabled()) {
logger.info(referenceBean + " was destroying!");
}
referenceBean.destroy();
}

injectionMetadataCache.clear();
referenceBeansCache.clear();

if (logger.isInfoEnabled()) {
logger.info(getClass() + " was destroying!");
}

}

@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

/**
* {@link Reference} {@link Method} {@link InjectionMetadata.InjectedElement}
Expand Down Expand Up @@ -304,10 +321,9 @@ private Object buildReferenceBean(Reference reference, Class<?> referenceClass)

if (referenceBean == null) {

ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder.create();
beanBuilder.setReference(reference);
beanBuilder.setReferenceClass(referenceClass);
beanBuilder.setApplicationContext(applicationContext);
ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder
.create(reference, classLoader, applicationContext)
.interfaceClass(referenceClass);

referenceBean = beanBuilder.build();

Expand Down
Loading

0 comments on commit 375cda0

Please sign in to comment.