如何在BeanFactoryPostProcessor或者BeanDefinitionRegistryPostProcessor中使用配置

BeanFactoryPostProcessor

子类BeanDefinitionRegistryPostProcessor,它是BeanDefinitionRegistry的后置处理器。

子类InstantiationAwareBeanPostProcessorAdapter

由于BeanFactoryPostProcessor的初始化是在Bean实例化之前,所以配置的Bean还没有注入属性,使用配置可以通过实现EnvironmentAware接口:

1
2
3
4
@Override
public void setEnvironment(Environment environment) {
resolver = new RelaxedPropertyResolver( environment, "hessian.server." );
}

注意:”hessian.server.” 后面的”.”

看下面代码,自动注册Bean的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @author Liu Hailin
* @create 2017-11-21 上午11:20
**/
@Configuration
@Slf4j
public class HessianServiceAutoConfiguration implements EnvironmentAware, BeanFactoryPostProcessor {

private RelaxedPropertyResolver resolver;

private Class<?> getServiceInterface(Class<?> beanClass) {

Class<?> serviceInterface = null;

if (AnnotationUtils.isAnnotationDeclaredLocally( HessianService.class, beanClass )) {

for (Class<?> interfaceClass : beanClass.getInterfaces()) {
if (AnnotationUtils.isAnnotationDeclaredLocally( HessianClient.class, interfaceClass )) {
serviceInterface = interfaceClass;
break;
}
}
}
return serviceInterface;
}

private Set<BeanDefinitionHolder> scanHessianService(BeanDefinitionRegistry beanDefinitionRegistry) {

String classPath = HessianServiceAutoConfiguration.class.getClassLoader().getResource( "" ).getPath();

String basePackage = resolver.getProperty( "basePackage" );
String[] basePackages = StringUtils.tokenizeToStringArray( basePackage, ";" );

if (basePackage == null) {
log.warn( "!!!You has not set Hassian Service Scan basePackage. use default:{}", classPath );
}

HessianServiceScanner serviceScanner = new HessianServiceScanner( beanDefinitionRegistry );

//service 已经注册了
return serviceScanner.doScan( basePackages );
}

@Override
public void setEnvironment(Environment environment) {
resolver = new RelaxedPropertyResolver( environment, "hessian.server." );
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory)
throws BeansException {
Set<BeanDefinitionHolder> serviceHolder = scanHessianService(
(BeanDefinitionRegistry)configurableListableBeanFactory );

for (BeanDefinitionHolder holder : serviceHolder) {

String beanName = holder.getBeanName();

Object beanEntity = configurableListableBeanFactory.getBean( beanName );

BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(
HessianServiceExporter.class );
beanDefinitionBuilder.addPropertyReference( "service", beanName );
beanDefinitionBuilder.addPropertyValue( "serviceInterface", getServiceInterface( beanEntity.getClass() ) );

String publisherName = "/" + (beanName + "_provider").hashCode();

((BeanDefinitionRegistry)configurableListableBeanFactory).registerBeanDefinition( publisherName,
beanDefinitionBuilder.getBeanDefinition() );
}
}
}
坚持技术分享,您的支持将鼓励我继续创作!