Bean的生命周期是指Bean的创建、初始化、销毁的过程。
(1) @Bean指定initMethod和destroyMethod
①实体类
public class Car {
public Car() {
System.out.println("Car构造方法");
}
public void init () {
System.out.println("Car初始化");
}
public void destory () {
System.out.println("Car销毁");
}
}
②配置类
import com.it.huaxin.vo.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CarConfig {
@Bean(initMethod="init", destroyMethod = "destory")
public Car car () {
return new Car();
}
}
③测试类
import com.it.huaxin.config.CarConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class CarTest {
public static void main(String [] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(CarConfig.class);
System.out.println("容器创建完成");
applicationContext.close();
}
}
//结果
Car构造方法
Car初始化
容器创建完成
Car销毁
(2)让Bean实现InitializingBean和DisposableBean接口,分别重写afterPropertiesSet()和destroy()。
①实体类
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Car implements InitializingBean, DisposableBean {
public Car() {
System.out.println("Car构造方法");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Car初始化");
}
@Override
public void destroy() throws Exception {
System.out.println("Car销毁");
}
}
②配置类
import com.it.huaxin.vo.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CarConfig {
@Bean
public Car car () {
return new Car();
}
}
③测试类
import com.it.huaxin.config.CarConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class CarTest {
public static void main(String [] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(CarConfig.class);
System.out.println("容器创建完成");
applicationContext.close();
}
}
// 结果
Car构造方法
Car初始化
容器创建完成
Car销毁
(3) 使用JSR250规范定义的两个注解@PostConstruct和@PreDestroy
①实体类
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Jeep { public Jeep() { System.out.println("Jeep构造方法"); } @PostConstruct public void init () { System.out.println("Jeep初始化"); } @PreDestroy public void destory () { System.out.println("Jeep销毁"); } }
(2)配置类
import com.it.huaxin.vo.Jeep; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JeepConfig { @Bean public Jeep jeep () { return new Jeep(); } }
(3)测试类
import com.it.huaxin.config.JeepConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class JeepTest { public static void main(String [] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(JeepConfig.class); System.out.println("容器创建完成"); applicationContext.close(); } }
//结果
Jeep构造方法
Jeep初始化
容器创建完成
Jeep销毁