/
MyBeanFactoryPostProcessor.java
58 lines (50 loc) · 2.63 KB
/
MyBeanFactoryPostProcessor.java
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
package com.dianping.start;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
/**
* Created with IntelliJ IDEA.
* Author: liangjun.zhong
* Date: 14-2-27
* Time: PM9:34
* To change this template use File | Settings | File Templates.
*/
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("postProcessBeanFactory---------begin");
System.out.println("decide to process myBean");
BeanDefinition myBeanDefinition = configurableListableBeanFactory.getBeanDefinition("myBean");
if(myBeanDefinition!=null){
System.out.println("myBeanDefinition.isLazyInit() : "+myBeanDefinition.isLazyInit());
System.out.println("myBeanDefinition.isSingleton() : "+myBeanDefinition.isSingleton());
MutablePropertyValues propertyValues = myBeanDefinition.getPropertyValues();
PropertyValue propertyValue = propertyValues.getPropertyValue("sex");
propertyValue.setConvertedValue("guess ni mei");
System.out.println("modify myBean sex");
}
String[] beanDefinitionNames = configurableListableBeanFactory.getBeanDefinitionNames();
if (beanDefinitionNames!=null && beanDefinitionNames.length>0) {
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(beanDefinitionName);
if ( beanDefinition.isSingleton() ) {
if ( beanDefinitionName.equals("singletonBeanUpdateTest") ) {
String beanClassName = beanDefinition.getBeanClassName();
try {
Class c = Class.forName(beanClassName);
if ( SingletonBeanUpdateTest.class.isAssignableFrom(c) ) {
beanDefinition.setScope("prototype");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
}
System.out.println("postProcessBeanFactory---------end");
}
}