春のアノテーション主導の開発[02-ライフサイクル]

春のアノテーション主導の開発[ライフサイクル]

1. @Beanは、初期化と破棄の方法を指定します

Beanのライフサイクル

bean的生命周期:
    创建 -> 初始化 -> 销毁

スプリングが注入された後、コンテナはBeanのライフサイクルを管理し
ます。初期化と破棄のメソッドをカスタマイズできます。コンテナは、Beanが現在のライフサイクルに達すると、カスタムの初期化と破棄のメソッドを呼び出します。

1.1.init-メソッド与destroyMethod

xml構成ファイルでBeanを定義するときに、init-method = "" destroy-method = ""を追加します

または

@Bean(initMethod = ""、destroyMethod = "")によって定義されます

  • エンティティクラスCarを定義します
public class Car {
    
    
    public Car() {
    
    
        System.out.println("Car constructor...");
    }
//	定义初始化方法
    public void init(){
    
    
        System.out.println("Car init...");
    }
//	定义销毁方法
    public void destroy(){
    
    
        System.out.println("Car destroy...");
    }
}
  • 構成クラスを定義する
@Configuration
public class ConfigOfLifeCycle {
    
    
        @Bean(initMethod = "init" , destroyMethod = "destroy")
        public Car car(){
    
    
            return new Car();
        }
}
  • テストクラスによるテスト
public class MyTest02 {
    
    
    @Test
    public void test(){
    
    
//        创建ioc容器 (单例bean会在创建容器的同时就被实例化,同时进行初始化)
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigOfLifeCycle.class);
        System.out.println("容器创建完成....");
//        关闭ioc容器会将所有单例bean一同销毁
        context.close();
        System.out.println("容器销毁结束...");
    }
}

動作結果:

Car constructor...	
Car init...
容器创建完成....
Car destroy...
容器销毁结束...
  • 注入された車のスコープ
@Scope("prototype")
@Bean(initMethod = "init" , destroyMethod = "destroy")
  • もう一度テストする
容器创建完成....
容器销毁结束...

総括する:

bean的生命周期:
    创建 -> 初始化 -> 销毁
        创建:单例bean会在创建容器的同时就被实例化
             多实例bean会在每次获取的时候创建对象
        初始化:对象创建完成并赋值好后 调用初始化方法 多实例bean会在调用bean时进行初始化
              通常情况下 可以在初始化时为容器配置一些数据源或初始化数据等
        销毁:单例bean容器关闭时进行销毁 多实例bean的销毁不会被spring容器接管
              通常情况下 可以在销毁时做一些收尾工作 例如关闭一些连接等

1.2。エンティティクラスにInitializingBeanとDisposableBeanを実装させることにより、初期化と破棄をカスタマイズします

  • エンティティクラスをカスタマイズし、Springが提供するInitializingBean(初期化)およびDisposableBean(破棄)インターフェースを実装します。
@Component	//要实现ComponentScan进行组件扫描 
public class Cat implements DisposableBean, InitializingBean {
    
    
    public Cat() {
    
    
        System.out.println("Cat constructor...");
    }

    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println("Cat afterPropertiesSet....");
    }

    public void destroy() throws Exception {
    
    
        System.out.println("Cat destory...");
    }

}

  • スキャンコンポーネントの注釈を構成クラスに追加します

    @ComponentScan(value = "indi.zhihuali.pojo")
    public class ConfigOfLifeCycle {
          
          
        ...
    }
    
  • テスト

    @Test
    public void test(){
    
    
//        创建ioc容器 (单例bean会在创建容器的同时就被实例化)
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigOfLifeCycle.class);
        System.out.println("容器创建完成....");
//        关闭ioc容器会将所有单例bean一同销毁
        context.close();
        System.out.println("容器销毁结束...");
    }
}

動作結果:

Cat constructor...
Cat afterPropertiesSet....
Car constructor...
Car init...
容器创建完成....
Car destroy...
Cat destory...
容器销毁结束...

カスタムの初期化と破棄の構成も実行できることがわかります

1.3.Springで定義されたJSR250アノテーションを使用する

@PostConstruct:在bean创建完成并属性赋值完成时 执行初始化方法
@PreDestroy:在容器销毁bean前通知我们完成清理工作
  • カスタムpojoエンティティクラスと登録iocBean

    @Component
    public class Dog {
          
          
        public Dog() {
          
          
            System.out.println("dog constructor...");
        }
        
    //    对象创建并赋值后调用
        @PostConstruct
        public void init(){
          
          
            System.out.println("dog @PostConstruct....init....");
        }
        
    //    对象移除之前
        @PreDestroy
        public void destroy(){
          
          
            System.out.println("dog @PreDestroy...destroy...");
        }
    }
    
  • テスト

@Test
    public void test(){
    
    
//        创建ioc容器 (单例bean会在创建容器的同时就被实例化)
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigOfLifeCycle.class);
        System.out.println("容器创建完成....");
//        关闭ioc容器会将所有单例bean一同销毁
        context.close();
        System.out.println("容器销毁结束...");
    }

出力:

dog constructor....
dog @PostConstruct....init....
容器创建完成....
dog @PreDestroy...destroy...
容器销毁结束...

2.BeanPostProcessorポストプロセッサ

2.1。BeanPostProcessorインターフェースの使用

BeanPostProcessor【interface】:bean的后置处理器
  postProcessBeforeInitialization:初始化前调用
  postProcessAfterInitialization: 初始化后调用

BeanPostProcessorインターフェースを実装するクラスをカスタマイズし、Beanを登録します

@Component
//      后置处理器:初始化前后进行工作
public class MyBeanPostProcessor implements BeanPostProcessor {
    
    
//   	bean初始化前工作
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("postProcessBeforeInitialization..."+bean+" name:"+beanName);
        return bean;
    }

//   	bean初始化后工作
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("postProcessAfterInitialization..."+bean+" name:"+beanName);
        return bean;
    }
}

  • テスト
@Test
    public void test(){
    
    
//        创建ioc容器 (单例bean会在创建容器的同时就被实例化)
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigOfLifeCycle.class);
        System.out.println("容器创建完成....");
//        关闭ioc容器会将所有单例bean一同销毁
        context.close();
        System.out.println("容器销毁结束...");
    }

結果は次のとおりです。

<!--spring内部组件初始化前后也同样调用了这两个方法-->
postProcessBeforeInitialization...indi.zhihuali.config.ConfigOfLifeCycle$$EnhancerBySpringCGLIB$$4dd9ed7c@1d016c9 name:configOfLifeCycle
postProcessAfterInitialization...indi.zhihuali.config.ConfigOfLifeCycle$$EnhancerBySpringCGLIB$$4dd9ed7c@1d016c9 name:configOfLifeCycle

<!--自定义bean实现该两种方法-->
<!-- Cat创建 -->
Cat constructor...
<!-- Cat初始化前 -->
postProcessBeforeInitialization...indi.zhihuali.pojo.Cat@167e60c name:cat
<!-- 通过1.2中的方法对Cat进行初始化 -->
Cat afterPropertiesSet....
<!-- Cat初始化结束 -->
postProcessAfterInitialization...indi.zhihuali.pojo.Cat@167e60c name:cat
<!-- Dog创建 -->
dog constructor...
<!-- Dog初始化前 -->
postProcessBeforeInitialization...indi.zhihuali.pojo.Dog@ee5251 name:dog
<!-- 通过1.3中的方法对Dog进行初始化 -->
dog @PostConstruct....init....
<!-- Dog初始化结束 -->
postProcessAfterInitialization...indi.zhihuali.pojo.Dog@ee5251 name:dog
Car constructor...
postProcessBeforeInitialization...indi.zhihuali.pojo.Car@23e5ee name:car
Car init...
postProcessAfterInitialization...indi.zhihuali.pojo.Car@23e5ee name:car
容器创建完成....
Car destroy...
dog @PreDestroy...destroy...
Cat destory...
容器销毁结束...

したがって、BeanPostProcessorを追加した後のBeanのライフサイクルは次のように分割できます。

bean的生命周期:
    创建 -> 初始化 -> 销毁
        创建:单例bean会在创建容器的同时就被实例化
             多实例bean会在每次获取的时候创建对象
        BeanPostProcessor.postProcessBeforeInitialization:初始化前调用   
        初始化:对象创建完成并赋值好后 调用初始化方法 多实例bean会在调用bean时进行初始化
              通常情况下 可以在初始化时为容器配置一些数据源或初始化数据等
        BeanPostProcessor.postProcessAfterInitialization: 初始化后调用  
        销毁:单例bean容器关闭时进行销毁 多实例bean的销毁不会被spring容器接管
              通常情况下 可以在销毁时做一些收尾工作 例如关闭一些连接等

2.2.BeanPostProcessorインターフェースの原理

カスタムBeanPostProcessorでpostProcessBeforeInitializationとpostProcessAfterInitializationをデバッグします

ここに写真の説明を挿入

原理:(debug)
           遍历得到容器中所有的BeanPostProcessor 并逐个执行beforeInitialization
           一旦方法返回null,跳出for循环
           并且不会执行后边的 BeanPostProcessor 的 applyBeanPostProcessorsBeforeInitialization方法

       先执行:
           populateBean(beanName, mbd, instanceWrapper);   给bean属性赋值
       其次:
           initBean{
                applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
                invokeInitMethods(beanName, wrappedBean, mbd);	执行初始化
                applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
    

2.3。Springの下部でのBeanPostProcessorの使用

次のシナリオで使用されます。

  1. Beanの割り当てにより、他のコンポーネントが挿入されます

  2. @Autowired

  3. ライフサイクルアノテーション機能

  4. @Async非同期関数

おすすめ

転載: blog.csdn.net/weixin_45729934/article/details/108685155