CommonAnnotationBeanPostProcessor

111 阅读1分钟

@Component
public class PostConstructTest {

    @PostConstruct
    public void init(){

    }

    @PreDestroy
    public void destroy(){

    }
}
@Resource
@PostConstructor
@preDestroy是jdk提供的
@Autowired是spring提供的

问题?

MergedBeanDefinitionPostProcessor的子类CommonAnnotationBeanPostProcessor,
其中的postProcessMergedBeanDefinition方法调用
父类InitDestroyAnnotationBeanPostProcessor的postProcessMergedBeanDefinition的方法,
这个方法会调用其内部的findLifecycleMetadata方法,
这个内部方法当中也调用了另一个方法buildLifecycleMetadata,
在其中有this.initAnnotationType,this.destroyAnnotationType。
为什么this.initAnnotationType指代PostConstruct,this.destroyAnnotationType指代destroy?

8.png

解答

/**
 * 构造方法,设置@PostConstruct和@PreDestory注解
 */
public CommonAnnotationBeanPostProcessor() {
   setOrder(Ordered.LOWEST_PRECEDENCE - 3);
   setInitAnnotationType(PostConstruct.class);
   setDestroyAnnotationType(PreDestroy.class);
   ignoreResourceType("javax.xml.ws.WebServiceContext");
}