springのbeanのライフサイクル

5429 ワード

ライフサイクルのプロセス:
spring容器管理beanのライフサイクル
ベーンの作成——初期化——廃棄
私達もカスタム初期化と廃棄方法を通じて(通って)ことができます.現在のライフサイクルに容器が入った時に、カスタマイズした初期化と廃棄方法を呼び出します.
1)初期化と廃棄方法を指定する
beanの実体クラス:
public class Blue {
    public Blue(){
        System.out.println("       ...");
    }
    public void init(){
        System.out.println("        ...");
    }
    public void destroy(){
        System.out.println("       ");
    }
}
MainConfig配置類:
@Bean(initMethod = "init",destroyMethod = "destroy")
public Blue blue(){
    return new Blue();
}
テストクラス:
public class IocTest {
    private AnnotationConfigApplicationContext applicationContext;

    @Before
    public void init(){
        applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        System.out.println("       ...");
    }
    @Test
    public void testLifeCycle(){
        applicationContext.getBean("blue");
        applicationContext.close();
    }
}
テスト結果:
   10, 2019 5:05:37    org.springframework.context.support.AbstractApplicationContext prepareRefresh
  : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Tue Sep 10 17:05:37 CST 2019]; root of context hierarchy
       ...
        ...
       ...

   10, 2019 5:05:37    org.springframework.context.support.AbstractApplicationContext doClose
       
結論:指定された初期化方法はbeanオブジェクトの作成が完了したら呼び出されます.廃棄方法は容器が閉まる時に呼び出されます.
ちなみに、beanオブジェクトが複数例の場合、springはその廃棄方法を呼び出さず、対象の廃棄はJVMによって回収される.
2)InitializingBenとDispposable Bean
役割:私たちはBeanにInitializingBean(初期化ロジックを定義)とDispsable Beanを実現することができます(廃棄ロジックを定義します).
Cat実体類
@Component
public class Cat implements InitializingBean,DisposableBean {
    public Cat(){
        System.out.println("       ...");
    }
    public void destroy() throws Exception {
        System.out.println("destroy...");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet...");
    }
}
テストクラス
public class IocTest {
    private AnnotationConfigApplicationContext applicationContext;

    @Before
    public void init(){
        applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        System.out.println("       ...");
    }
    @Test
    public void testLifeCycle(){
        applicationContext.getBean("cat");
        applicationContext.close();
    }
}
テスト結果
  : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Tue Sep 10 17:26:30 CST 2019]; root of context hierarchy
       ...
afterPropertiesSet...
       ...
   10, 2019 5:26:30    org.springframework.context.support.AbstractApplicationContext doClose
  : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Tue Sep 10 17:26:30 CST 2019]; root of context hierarchy
destroy...
3)JSR 250
作用:ここでは@PostConstruct注解を使って、初期化されたフィードバックの代替として@Predestry注解を廃棄フィードバックの代替として使うことができます.
ビーン本体類
@Component
public class Blue {
    public Blue(){
        System.out.println("       ...");
    }
    //           
    @PostConstruct
    public void init(){
        System.out.println("init...   ");
    }
    //  Bean    
    @PreDestroy
    public void destroy(){
        System.out.println("destroy...   ");
    }
}
テストクラス
public class IocTest {
    private AnnotationConfigApplicationContext applicationContext;

    @Before
    public void init(){
        applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        System.out.println("       ...");
    }
    @Test
    public void testLifeCycle(){
        applicationContext.getBean("blue");
        applicationContext.close();
    }
}
テスト結果
   10, 2019 7:41:06    org.springframework.context.support.AbstractApplicationContext prepareRefresh
  : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Tue Sep 10 19:41:06 CST 2019]; root of context hierarchy
       ...
init...   
   10, 2019 7:41:06    org.springframework.context.support.AbstractApplicationContext doClose
  : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Tue Sep 10 19:41:06 CST 2019]; root of context hierarchy
       ...
destroy...   
4)BenPostProcessor後置プロセッサ
役割:ビーンPostProcessorの実装クラスをカスタマイズして、ビーンオブジェクトの初期化前後を操作することができます.
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{
    /**
     *
     * @param bean          
     * @param beanName            
     * @return          bean          
     * @throws BeansException
     */
    //bean            
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization...   ");
        return bean;
    }
    //bean            
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization...   ");
        return bean;
    }
}