生命始末:
applicationcontext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注册Service -->
<bean id="myService" class="com.bjpowernode.ba06.SomeServiceImpl"
init-method="setUp" destroy-method="tearDown"/>
</beans>
接口:
public interface ISomeService {
void doSome();
}
实现类:
public class SomeServiceImpl implements ISomeService { public SomeServiceImpl() { System.out.println("执行无参构造器"); } @Override public void doSome() { System.out.println("执行doSome()方法"); } public void setUp() { System.out.println("生命起始"); } public void tearDown() { System.out.println("销毁之前"); } }
测试类:
public class MyTest { @Test public void test01() { // 创建容器对象,加载Spring配置文件 String resource = "com/bjpowernode/ba06/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); ISomeService service = (ISomeService) ac.getBean("myService"); service.doSome(); // 对于销毁方法的执行,有两个条件: // 1)当前的Bean需要是singleton的 // 2)要手工关闭容器 ((ClassPathXmlApplicationContext)ac).close(); } }
生命周期:
applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 注册Service --> <bean id="myService" class="com.bjpowernode.ba07.SomeServiceImpl" init-method="setUp" destroy-method="tearDown"> <property name="adao" value="aaa"/> <property name="bdao" value="bbb"/> </bean> <bean class="com.bjpowernode.ba07.MyBeanPostProcessor"/> </beans>
处理类:
实现类public class MyBeanPostProcessor implements BeanPostProcessor { // bean:表示当前正在进行初始化的Bean对象 // beanName:表示当前正在进行初始化的Bean对象的id @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Step5:执行 ----before---()方法"); return bean; } @Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { System.out.println("Step8:执行 ----after---()方法"); return bean; } }
public class SomeServiceImpl implements ISomeService, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean { private String adao; private String bdao; public void setAdao(String adao) { this.adao = adao; System.out.println("Step2:执行setter"); } public void setBdao(String bdao) { this.bdao = bdao; System.out.println("Step2:执行setter"); } public SomeServiceImpl() { System.out.println("Step1:执行无参构造器"); } @Override public void doSome() { System.out.println("Step9:执行doSome()方法"); } public void setUp() { System.out.println("Step7:初始化完毕之后"); } public void tearDown() { System.out.println("Step11:销毁之前"); } @Override public void setBeanName(String name) { System.out.println("Step3:获取到bean的id = " + name); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("Step4:获取到BeanFactory容器"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("Step6:Bean初始化完毕了"); } @Override public void destroy() throws Exception { System.out.println("Step10:实现接口的销毁之前"); } }
接口:
public interface ISomeService { void doSome(); }
测试类:public class MyTest { @Test public void test01() { // 创建容器对象,加载Spring配置文件 String resource = "com/bjpowernode/ba07/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); ISomeService service = (ISomeService) ac.getBean("myService"); service.doSome(); // 对于销毁方法的执行,有两个条件: // 1)当前的Bean需要是singleton的 // 2)要手工关闭容器 ((ClassPathXmlApplicationContext)ac).close(); } }