spring容器中bean初始化或者销毁时会执行一些方法,有3种实现方式。
1、实现InitializingBean、DisposableBean接口,在bean的属性设置后和bean销毁时分别执行afterPropertiesSet和destroy方法
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>spring</artifactId> <version>1.0.0</version> <name>spring</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> </dependencies> </project>View Code
Cat.java
package com.edu.spring; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Cat implements InitializingBean,DisposableBean{ @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("-----------afterPropertiesSet--------------"); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("-----------destroy--------------"); } }View Code
MyConfig.java
package com.edu.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean public Cat createCat(){ return new Cat(); } }View Code
App.java
package com.edu.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class); System.out.println(context.getBean(Cat.class)); context.close(); } }View Code
运行结果如下:
2、在@Bean注解中用initMethod和destroyMethod两个属性指定实体类中的两个方法,这两个方法分别在bean初始化和销毁时候执行
Dog.java
package com.edu.spring; public class Dog { public void init(){ System.out.println("----------Dog init---------"); } public void destroy(){ System.out.println("----------Dog destroy---------"); } }View Code
MyConfig.java
package com.edu.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean(initMethod="init",destroyMethod="destroy") public Dog createDog(){ return new Dog(); } }View Code
App.java
package com.edu.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class); System.out.println(context.getBean(Dog.class)); context.close(); } }View Code
运行结果如下:
3、在实体类中方法上面分别添加@PostConstruct和@PreDestroy注解,分别指定初始化和销毁方法
Animal.java
package com.edu.spring; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Animal { @PostConstruct public void initial(){ System.out.println("-------initial---------"); } @PreDestroy public void close(){ System.out.println("---------close---------"); } }View Code
MyConfig.java
package com.edu.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean public Animal createAniaml(){ return new Animal(); } }View Code
App.java
package com.edu.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class); System.out.println(context.getBean(Animal.class)); context.close(); } }View Code
运行结果如下: