Spring 5.x Source trip thirty-seven getBean processor extensions thirty
Figure no less
applyMergedBeanDefinitionPostProcessors
After instantiation, the merger bean
definition, in fact, is to update bean
the definition of the matter, here again you can modify bean
the definition, there can only modify the RootBeanDefinition
type. As I mentioned before, mainly InitDestroyAnnotationBeanPostProcessor
, CommonAnnotationBeanPostProcessor
and AutowiredAnnotationBeanPostProcessor
processing methods have attributes or injection annotations. Respectively, to deal with the life cycle PostConstruct
, PreDestroy
notes,Resource,WebServiceRef,EJB注解
, Autowired
and Value
comments.
applyBeanPostProcessorsAfterInitialization
In fact, this can be that with the above, such as the above that do some processing, you can use the back of this, we simply do the following subclasses look.
Actual extension points
I want to define a comment, there is a property on the annotation, the number of times the method is to be printed, I hope to find a way to define all the attributes, and then print them.
MyAnnotation comment
Property count
is the number printed.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
int count() default 0;
}
MyMergedBean
We comment some ways, to see if he will not be printed.
@Component
public class MyMergedBean {
@MyAnnotation(count = 1)
public void m1(String msg) {
print(msg);
}
@MyAnnotation(count = 2)
public void m2(String msg) {
print(msg);
}
@MyAnnotation()
public void m3(String msg) {
print(msg);
}
private void print(String msg) {
System.out.println(msg);
}
}
MyMergedBeanDefinitionPostProcessor processor
This is mainly achieved postProcessMergedBeanDefinition
, the processing done after instantiation, and then after the initialization postProcessBeforeInitialization
specific processing. The main print annotated methods, properties annotated according to.
@Component
public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {
//bean名字对应的注解方法
public Map<String,List<Method>> stringMethodMap;
@Nullable
private Class<? extends Annotation> myAnnotationType;
public MyMergedBeanDefinitionPostProcessor(){
myAnnotationType=MyAnnotation.class;
stringMethodMap=new HashMap<>();
}
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
List<Method> list=new ArrayList<>();
ReflectionUtils.doWithLocalMethods(beanType, method -> {
if (this.myAnnotationType != null && method.isAnnotationPresent(this.myAnnotationType)) {
list.add(method);
stringMethodMap.put(beanName,list);
}
});
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(stringMethodMap.get(beanName)!=null){
for (Method method : stringMethodMap.get(beanName)) {
try {
MyAnnotation annotation = (MyAnnotation) method.getAnnotation(this.myAnnotationType);
for (int i = 0; i < annotation.count(); i++) {
method.invoke(bean,new Object[]{method.getName()});
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return bean;
}
}
Test category
@Test
public void MergedBeanDefinitionPostProcessorTest() throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(MyConfig.class);
applicationContext.refresh();
}
I would like to change to change:
Results:
Why is not the order of definition enforce it, seems to be because JDK
the reflection is out of disorder, if you can order CGLIB
the ClassVisitor
come up, the specific spring
source, there can refer to ConfigurationClassParser
the retrieveBeanMethodMetadata
:
Well, MyMergedBeanDefinitionPostProcessor
in the end what use is it, to see your business matter, as long as you want to do after the extension is instantiated, you can try this, participate in bean
the process of initialization.
Well, here today, we hope to help study and understand, do not spray the Great God see, understand only their own learning, limited capacity, please excuse.