SpringIOC随笔(五)-Bean下

Love The Way You Lie 2022-12-16 14:25 239阅读 0赞

SpringIOC随笔(五)-Bean下

  1. bean的生命周期

    1. init-method

      • Bean创建后自动调用的方法
    2. destroy-method

      • Bean销毁后自动调用的方法
      • public void myInit() {

        1. System.out.println("自定义init方法调用!");

        }
        public void myDestroy() {

        1. System.out.println("自定义destroy方法调用!");

        }

      • bean创建后自动调用myInit方法,在销毁时自动调用myDestroy方法。
    3. BeanNameAware接口的setBeanName方法

      • 该方法是让bean获取自己在beanfactory中的名称。
      • @Override
        public void setBeanName(String name) {

        1. System.out.println("setBeanName:" + name + "调用!");

        }

    4. BeanFactoryAware接口的setBeanFactory方法

      • 实现这个接口能通过这个方法获取当前的BeanFactory
      • @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

        1. System.out.println("setBeanFactory:" + beanFactory + "调用!");

        }

    5. ApplicationContextAware接口的setApplicationContext方法

      • 实现这个接口可以得到当前bean的spring上下文
      • @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        1. System.out.println("setApplicationContext:" + applicationContext);

        }

    6. InitializingBean接口的afterPropertiesSet方法

      • @Override
        public void afterPropertiesSet() throws Exception {

        1. System.out.println("afterPropertiesSet调用");

        }

    7. DisposableBean接口的destroy方法

      • @Override
        public void destroy() throws Exception {

        1. System.out.println("destory方法调用!");

        }

    8. BeanPostProcessor接口的两个方法(postProcessBeforeInitialization,postProcessAfterInitialization)

      • @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        1. System.out.println("postProcessBeforeInitialization: beanName=" + beanName);
        2. return bean;

        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        1. System.out.println("postProcessAfterInitialization: beanName=" + beanName);
        2. return bean;

        }

      • 这里有点意思的地方就是当这个bean实现了这个接口,那么这个bean就不会调用后置处理器了,也就是这两个方法,只有别的bean没有实现这个接口才会去调用别的实现了这个接口的bean的方法,而且没个bean都会调用一次这个方法,如果有多个bean实现了这个接口,那么这几个实现了后置处理器接口的bean的方法在bean初始化的时候,会调用每一个实现了这个接口的bean的这个方法。

      • 而且实现了这个接口的bean一定会比没实现这个接口的bean早初始化。

    9. 当这个Bean配置为原型的时候,DisposableBean接口的destroy方法和自定义的destroy方法全部失效,不会调用。

    10. 结合之前的,当Bean配置为原型时,DisposableBean接口的destroy方法和自定义的destroy方法全部失效,然后lazy-init=”false”也会失效。

    11. 代码

      • public class InitDemo1 implements Serializable, BeanNameAware, ApplicationContextAware, BeanFactoryAware, InitializingBean, DisposableBean {

        1. private static final long serialVersionUID = -2167328278489217537L;
        2. public InitDemo1() {
        3. System.out.println("InitDemo1构造方法调用!");
        4. }
        5. public void myInit() {
        6. System.out.println("自定义init方法调用!");
        7. }
        8. public void myDestroy() {
        9. System.out.println("自定义destroy方法调用!");
        10. }
        11. public void service() {
        12. System.out.println("service方法调用!");
        13. }
        14. @Override
        15. public void setBeanName(String name) {
        16. System.out.println("setBeanName:" + name + "调用!");
        17. }
        18. @Override
        19. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        20. System.out.println("setBeanFactory:" + beanFactory + "调用!");
        21. }

        // @Override

        1. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        2. System.out.println("postProcessBeforeInitialization: beanName=" + beanName);
        3. return bean;
        4. }

        // @Override

        1. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        2. System.out.println("postProcessAfterInitialization: beanName=" + beanName);
        3. return bean;
        4. }
        5. @Override
        6. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        7. System.out.println("setApplicationContext:" + applicationContext);
        8. }
        9. @Override
        10. public void afterPropertiesSet() throws Exception {
        11. System.out.println("afterPropertiesSet调用");
        12. }
        13. @Override
        14. public void destroy() throws Exception {
        15. System.out.println("destory方法调用!");
        16. }

        }
        public class InitDemo2 implements Serializable,BeanPostProcessor{

  1. private static final long serialVersionUID = 5867932081615656754L;
  2. public InitDemo2() {
  3. System.out.println("InitDemo2构造方法调用!");
  4. }
  5. public void service() {
  6. System.out.println("InitDemo2service方法调用!");
  7. }
  8. @Override
  9. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  10. System.out.println("postProcessBeforeInitialization: beanName=" + beanName);
  11. return bean;
  12. }
  13. @Override
  14. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  15. System.out.println("postProcessAfterInitialization: beanName=" + beanName);
  16. return bean;
  17. }
  18. }
  19. public class InitDemo3 implements Serializable, BeanPostProcessor {
  20. private static final long serialVersionUID = -3806397125776158318L;
  21. public InitDemo3() {
  22. System.out.println("InitDemo3构造方法调用!");
  23. }
  24. public void service() {
  25. System.out.println("InitDemo2service方法调用!");
  26. }
  27. @Override
  28. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  29. System.out.println("postProcessBeforeInitialization: beanName=" + beanName);
  30. return bean;
  31. }
  32. @Override
  33. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  34. System.out.println("postProcessAfterInitialization: beanName=" + beanName);
  35. return bean;
  36. }
  37. }
  38. *
  39. applicationContext-init.xml
  40. <?xml version="1.0" encoding="UTF-8"?>
  41. <beans xmlns="http://www.springframework.org/schema/beans"
  42. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  43. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  44. <bean id="initDemo1" class="com.fxyh.bean.InitDemo1" init-method="myInit" destroy-method="myDestroy" scope="prototype"/>
  45. <bean id="initDemo2" class="com.fxyh.bean.InitDemo2" />
  46. <bean id="initDemo3" class="com.fxyh.bean.InitDemo3"/>
  47. </beans>
  48. *
  49. public class InitDemo1Test {
  50. private AbstractApplicationContext context;
  51. private InitDemo1 initDemo1;
  52. private InitDemo2 initDemo2;
  53. @Before
  54. public void setUp() throws Exception {
  55. this.context = new ClassPathXmlApplicationContext("classpath*:applicationContext-init.xml");
  56. this.initDemo1 = this.context.getBean(InitDemo1.class);
  57. this.initDemo2 = this.context.getBean(InitDemo2.class);
  58. }
  59. @Test
  60. public void test(){
  61. this.initDemo1.service();
  62. this.initDemo2.service();
  63. }
  64. @After
  65. public void after(){
  66. this.context.close();
  67. }
  68. }
  69. 12. ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3ODcyNzky_size_16_color_FFFFFF_t_70_pic_center]
  1. 使用注解的方式:

    • @PostConstruct

      • 自定义init方法的注解
    • @PreDestroy

      • 自定义destroy方法的注解
    • 例子:

      • @Component
        public class InitDemoByAnnotation implements Serializable {

        private static final long serialVersionUID = -6995664151240989094L;

        public InitDemoByAnnotation() {

        1. System.out.println("InitDemoByAnnotation构造方法!");

        }

        @PostConstruct
        public void myInit(){

        1. System.out.println("自定义init方法!");

        }

        @PostConstruct
        public void myInit2(){

        1. System.out.println("自定义init2方法!");

        }

        @PreDestroy
        public void myDestroy(){

        1. System.out.println("自定义destroy方法!");

        }

        @PreDestroy
        public void myDestroy2(){

        1. System.out.println("自定义destroy2方法!");

        }

        }

      • <?xml version=”1.0” encoding=”UTF-8”?>





      • public class InitDemoByAnnotationTest {
  1. private AbstractApplicationContext context;
  2. private InitDemoByAnnotation initDemoByAnnotation;
  3. @Before
  4. public void setUp() throws Exception {
  5. this.context = new ClassPathXmlApplicationContext("classpath*:applicationContext-annotation.xml");
  6. this.initDemoByAnnotation = this.context.getBean(InitDemoByAnnotation.class);
  7. }
  8. @Test
  9. public void test(){
  10. System.out.println(initDemoByAnnotation);
  11. }
  12. @After
  13. public void after(){
  14. this.context.close();
  15. }
  16. }
  17. * 或者不使用xml,直接在test中把`new ClassPathXmlApplicationContext();`换成`this.context = new AnnotationConfigApplicationContext("com.fxyh.bean");`就好了。
  18. * #### 这里自定义的init方法和destroy方法可以写多个,在XML中好像没办法写多个,可能是我不知道的原因吧,使用注解的时候写多个的时候,调用顺序是随机的,反正我在开发中也没有这样搞过。 ####

发表评论

表情:
评论列表 (有 0 条评论,239人围观)

还没有评论,来说两句吧...

相关阅读