1.bean生命周期

1.生命周期步骤

1.通过构造器创建Bean实例
2.为bean注入属性(set),
3.把Bean传递bean前置处理器postProcessBeforeInitialization()
4.调用Bean中初始化方法(需要配置)
5.把Bean传递bean后置处理器postProcessAfterInitialization()
6.获取到Bean对象
7.当容器关闭调用Bean的销毁方法(需要配置)

2.指定初始化和销毁方法的几种方式

  1. 通过@Bean指定init-methed和destroy-methed属性
  2. 让Bean类实现InitializingBean和DisposableBean接口
  3. 在方法上使用@PostConstruct(在bean创建完成和属性赋值完成时),@PreDestroy(在容器销毁之前)

    3. 生命周期源码

    image.png
    image.png
    多个处理器时,如果上一个返回null,则后面的处理器也不会执行
    image.png

    4.实验代码

    ```java package com.spring5.Bean;

/**

  • @Author rainbow
  • @Date 2022/6/17 15:49
  • @Version 1.0 **/ public class Cat { public Cat() {
    1. System.out.println("cat 被创建");
    } public void init(){
    1. System.out.println("init");
    } public void Destroy(){
    1. System.out.println("Destroy");
    } }
  1. ```java
  2. package com.spring5.Bean;
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5. /**
  6. * @Author rainbow
  7. * @Date 2022/6/17 15:51
  8. * @Version 1.0
  9. **/
  10. public class MyPostProcessor implements BeanPostProcessor {
  11. @Override
  12. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  13. if (beanName.equals("cat"))
  14. // System.out.println(beanName);
  15. System.out.println("前置处理器执行");
  16. return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
  17. }
  18. @Override
  19. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  20. if (beanName.equals("cat"))
  21. System.out.println("后置处理器执行");
  22. return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
  23. }
  24. }
  1. package com.spring5.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.pool.DruidDataSourceFactory;
  4. import com.spring5.Bean.Cat;
  5. import com.spring5.Bean.MyPostProcessor;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.jdbc.core.JdbcTemplate;
  10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  11. import org.springframework.transaction.annotation.EnableTransactionManagement;
  12. import javax.sql.DataSource;
  13. import java.io.InputStream;
  14. import java.util.Properties;
  15. @Configuration //表示为配置类
  16. public class TxConfig {
  17. //创建bean并指定初始化和销毁方法
  18. @Bean(initMethod = "init", destroyMethod = "Destroy",name = "cat")
  19. public Cat getCat() {
  20. return new Cat();
  21. }
  22. //将自定义后置处理器交给容器管理
  23. @Bean
  24. public MyPostProcessor getMyPostProcessor(){
  25. return new MyPostProcessor();
  26. }
  27. }

2.bean的作用域

  1. singleton单实例模式,容器会自动生成bean
  2. prototype多实例模式,每次返回的都是新的实例,只有手动获取bean时,容器才会生成bean
  3. request:仅对http请求起作用,每次请求都会创建新的bean
  4. session:同一个session共享一个bean
  5. global-session所有session共享一个bean