Spring对Bean的生命周期对的操作提供了支持,在使用JAVA配置和注解配置下提供如下两种方式:
(1)Java配置方式:使用@Bean的initMethod和destoryMethod。(相当于xml配置的init-method和destory-method)
(2)注解方式:利用JSR-250的@PostConstruct和@PreDestory。
Java配置方式演示
(1)BeanWayService类
package my.spring.service;
public class BeanWayService {
public void init() {
System.out.println("BeanWayService 的初始化方法");
}
public BeanWayService() {
System.out.println("BeanWayService 的构造方法");
}
public void destory() {
System.out.println("BeanWayService 的销毁方法");
}
}
(2)Config类
package my.spring.configuration;
import my.spring.service.BeanWayService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("my.spring")
public class Config {
@Bean(initMethod = "init",destroyMethod = "destory")
public BeanWayService beanWayService() {
return new BeanWayService();
}
}
(3)运行
package my.spring;
import my.spring.configuration.Config;
import my.spring.service.BeanWayService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// new一个AnnotationConfigApplicationContext对象context
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
// 从容器中获得对象
BeanWayService beanWayService = context.getBean(BeanWayService.class);
context.close();
}
}
运行结果:
注解方式演示
(1)添加JSR250依赖
<!-- JSR250依赖-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
(2)JSR250WayService类
package my.spring.service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class JSR250WayService {
@PostConstruct
public void init() {
System.out.println("JSR250WayService 的初始化方法");
}
public JSR250WayService() {
System.out.println("JSR250WayService 的构造方法");
}
@PreDestroy
public void destory() {
System.out.println("JSR250WayService 的销毁方法");
}
}
(3)Config类
package my.spring.configuration;
import my.spring.service.JSR250WayService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("my.spring")
public class Config {
@Bean(initMethod = "init",destroyMethod = "destory")
public JSR250WayService jsr250WayService() {
return new JSR250WayService();
}
}
(4)运行
package my.spring;
import my.spring.configuration.Config;
import my.spring.service.JSR250WayService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// new一个AnnotationConfigApplicationContext对象context
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
// 从容器中获得对象
JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
context.close();
}
}
运行结果:
总结
① initMethod指定BeanWayService类的init方法在构造方法之后执行,
destoryMethod指定BeanWayService类的destory方法在Bean销毁之前执行。
② @PostConstruct,在构造方法执行完执行之后执行,
@PreDestory,在Bean销毁之前执行。