欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring 初始化顺序 @PostConstruct

程序员文章站 2022-07-13 21:06:13
...

 

XML  pom.xml 配置

 

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-core</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
    </dependencies>
</project>

 

 

配置类

 

 

@Configuration
@ComponentScan("com.core")
@PropertySource("classpath:application.properties")
public class MainConfig {

}

 

 

 application.properties

 

spring.core.profile=prod

 

 

启动类

 

 public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

        UserService userService = applicationContext.getBean(UserService.class);
        System.out.println("获取到bean:"+userService);

        applicationContext.close();
    }

 

 

UserService

 

 

@Service
public class UserService implements InitializingBean , DisposableBean {


    @Value("${spring.core.profile}")
    private String profile;

    public UserService(){
        System.out.println("对象实例化"+this.toString());
    }

    @PostConstruct
    public void postConstruct(){
        System.out.println("初始化->postConstruct..."+this.toString());
    }

    @PreDestroy
    public void preDestory(){
        System.out.println("准备销毁->preDestory..."+this.toString());
    }

    //属性设置完之后
    public void afterPropertiesSet() throws Exception {
        this.profile = "prod_modify";
        System.out.println("InitializingBean->afterPropertiesSet...."+this.toString());

    }

    //applicationContext销毁之前
    public void destroy() throws Exception {
        System.out.println("准备销毁->DisposableBean->destroy....");
    }

    @Override
    public String toString() {
        return "UserService{" +
                "profile='" + profile + '\'' +
                '}';
    }
}

 

 

spring bean的初始化有好几种方式

 1 @PostConstruct

 2. init-method

 3.实现InitializingBean接口

 

输出结果

 

对象实例化UserService{profile='null'}
初始化->postConstruct...UserService{profile='prod'}
InitializingBean->afterPropertiesSet....UserService{profile='prod_modify'}
获取到bean:UserService{profile='prod_modify'}
准备销毁->preDestory...UserService{profile='prod_modify'}
准备销毁->DisposableBean->destroy....

 

 

结论:

1. 首先实例化对象UserService(属性未赋值为null)

2.在执行@PostConstruct 注解标注的初始化方法(初始化之前已经给对象属性赋值完毕)

3.在回调InitializingBean接口的afterPropertiesSet方法 (初始化方法以及运行,可以进一步修改对象的属性值)

4.获取到bean

5.spring上线文销毁 先调用 @preDestory注解标注的方法 在回调DisposableBean接口的destroy方法

 

注:

实例化和初始化的区别

1. 实例化是new 一个对象出来 会调用构造方法

2. 初始化是调用new出来对象的初始化方法

 

扩展点:

除了上面的几种方式spring还提供了修改bean的一种方式

是写BeanPostProcessor接口 , 改接口是针对spring容器中所有bean的修改

 

 public Object postProcessBeforeInitialization(Object bean, String beanName)  throws BeansException {
        return null;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)  throws BeansException {
        return null;
    }

 会实现这2个方法,

 

第一个参数就是srping容器中每个对象 

第二个参数是spring容器中每个对象的名字

拿到这些对象后可以做一些自己想做的事