Spring源码解析(八)——生命周期——BeanPostProcessor在spring底层的使用

一、ApplicationContextAwareProcessor

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class Dog implements ApplicationContextAware{

    private ApplicationContext applicationContext;

    public Dog(){
        System.out.println("dog constructor...");
    }

    //对象创建并赋值之后调用
    @PostConstruct
    public void init(){
        System.out.println("Dog....@PostConstruct...");
    }

    //容器移除对象之前
    @PreDestroy
    public void detory(){
        System.out.println("Dog....@PreDestroy...");
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

Dog类实现ApplicationContextAware接口,以此注入spring容器。靠的就是ApplicationContextAwareProcessor。

二、BeanValidationPostProcessor

三、InitDestroyAnnotationBeanPostProcessor

还记得我们的@PostConstruce和@PreDestory两个注解吗?它们是怎么实现的?

四、AutowiredAnnotationBeanPostProcessor

猜你喜欢

转载自blog.csdn.net/csdn_kenneth/article/details/83477917