一、实现功能
通过注解的方法,实现java Bean的初始化以及销毁方法。
二、环境
1.Pom
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
三、具体实现
1. 方法一:通过接口实现
(1)TestBean 类
package com.spring.ioc;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2019/10/27.
*/
@Component
public class TestBean implements InitializingBean,DisposableBean{
public void destroy() throws Exception {
System.out.println("TestBean.destroy");
}
public void afterPropertiesSet() throws Exception {
System.out.println("TestBean.afterPropertiesSet");
}
}
(2)TestConfiguration 类
package com.spring.ioc;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2019/10/27.
*/
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {
}
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
/**
* Created by Administrator on 2019/10/27.
*/
public class TestCode {
@Test
public void test(){
AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean=context.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);
context.close();
}
}
结果:
TestBean.afterPropertiesSet
testBean = com.spring.ioc.TestBean@123f1134
TestBean.destroy
2.方法二:直接在TestBean类中定义方法
(1)TestBean 类
package com.spring.ioc;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Created by Administrator on 2019/10/27.
*/
@Component
public class TestBean implements InitializingBean,DisposableBean{
public void destroy() throws Exception {
System.out.println("TestBean.destroy");
}
public void afterPropertiesSet() throws Exception {
System.out.println("TestBean.afterPropertiesSet");
}
@PostConstruct
public void onInit(){
System.out.println("TestBean onInit");
}
@PreDestroy
public void onDestroy(){
System.out.println("TestBean onDestroy");
}
}
(2)TestConfiguration 类
package com.spring.ioc;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2019/10/27.
*/
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {
}
(3)测试
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
/**
* Created by Administrator on 2019/10/27.
*/
public class TestCode {
@Test
public void test(){
AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean=context.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);
context.close();
}
}
结果
TestBean onInit
TestBean.afterPropertiesSet
testBean = com.spring.ioc.TestBean@2c34f934
TestBean onDestroy
TestBean.destroy
3.方法三:自定义方法,方法不需要注解(只用于@Bean)
备注:这个需要使用@Bean注解,而@Component注解是没有这两个方法的。
(1)TestBean :添加方法,并且去除@Component方式,而使用方法方式交由spring管理
package com.spring.ioc;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Created by Administrator on 2019/10/27.
*/
//@Component
public class TestBean implements InitializingBean,DisposableBean{
public void destroy() throws Exception {
System.out.println("TestBean.destroy");
}
public void afterPropertiesSet() throws Exception {
System.out.println("TestBean.afterPropertiesSet");
}
@PostConstruct
public void onInit(){
System.out.println("TestBean onInit");
}
@PreDestroy
public void onDestroy(){
System.out.println("TestBean onDestroy");
}
public void onInit1(){
System.out.println("TestBean onInit1");
}
public void onDestroy1(){
System.out.println("TestBean onDestroy1");
}
}
(2)TestConfiguration 类
package com.spring.ioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2019/10/27.
*/
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {
@Bean(initMethod = "onInit1",destroyMethod = "onDestroy1")
public TestBean testBean(){
return new TestBean();
}
}
(4) 测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
/**
* Created by Administrator on 2019/10/27.
*/
public class TestCode {
@Test
public void test(){
AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean=context.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);
context.close();
}
}
结果:
TestBean onInit
TestBean.afterPropertiesSet
TestBean onInit1
testBean = com.spring.ioc.TestBean@14a2f921
TestBean onDestroy
TestBean.destroy
TestBean onDestroy1