在我们进行实际开发的时候,经常遇到在Bean之前使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持。在使用Java配置和注解配置下提供如下三种方式:
1. Java配置方式:使用@Bean的initMethod和destroyMethod(相当于xml配置的init-method和destory-method):
① initMethod和destroyMethod指定BeanWayService类的init和destroy方法在构造函数之后,Bean销毁之前执行。
@Configuration
@ComponentScan("org.light4j.sping4.usually.prepost")
public class PrePostConfig {
@Bean(initMethod="init",destroyMethod="destroy") //①
BeanWayService beanWayService(){
return new BeanWayService();
}
@Bean
JSR250WayService jsr250WayService(){
return new JSR250WayService();
}
}
public class BeanWayService {
public voi