一、如何Bean的生命周期进行操作
Spring对Bean的生命周期操作提供了支持
java配置方式: 使用Bean的initMethod和destoryMethod进行配置
二、BeanWayDemo
Bean
package com.cactus.demo.beanway;
/**
* Created by liruigao
* Date: 2019-10-15 14:19
* Description:
*/
public class BeanWayService {
public void init() {
System.out.println("bean init!");
}
public void destory() {
System.out.println("bean destory!");
}
}
配置类
package com.cactus.demo.beanway;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by liruigao
* Date: 2019-10-15 14:21
* Description:
*/
@Configuration
@ComponentScan("com.cactus.demo.beanway")
public class BeanWayConfig {
@Bean(initMethod = "init", destroyMethod = "destory")
public BeanWayService getBeanWayService() {
return new BeanWayService();
}
}
Main
package com.cactus.demo.beanway;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by liruigao
* Date: 2019-10-15 14:23
* Description:
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanWayConfig.class);
BeanWayService beanWayService = context.getBean(BeanWayService.class);
context.close();
}
}
result