Step1:执行无参构造
Step2执行setter
Step2执行setter
Step3:获取到bean的id:myService
Step4:获取到beanFactory容器
Step5:执行---before---()方法
Step6:Bean初始化完毕了
Step7:初始化完毕之后
Step8:执行---after---()方法
ִStep9:执行doSome()
Step10:实现接口的销毁之前
Step2执行setter
Step2执行setter
Step3:获取到bean的id:myService
Step4:获取到beanFactory容器
Step5:执行---before---()方法
Step6:Bean初始化完毕了
Step7:初始化完毕之后
Step8:执行---after---()方法
ִStep9:执行doSome()
Step10:实现接口的销毁之前
Step11:销毁之前
package com.gqc.ba07; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext;
package com.gqc.ba07; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; public class MyTest { @Test public void test01() { //创建容器对象 加载Spring 配置文件 String resource = "com/gqc/ba07/appliactionContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(resource); ISomeService service = (ISomeService)ac.getBean("myService"); service.doSome(); //对于销毁方法的执行 有两个条件 //1、当前的bean 是singleton的 //2、需要关闭容器 ((ClassPathXmlApplicationContext)ac).close(); } }
import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.core.io.ClassPathResource;public class MyTest {@Testpublic void test01() {//创建容器对象 加载Spring 配置文件String resource = "com/gqc/ba07/appliactionContext.xml";ApplicationContext ac=new ClassPathXmlApplicationContext(resource);ISomeService service = (ISomeService)ac.getBean("myService");service.doSome();//对于销毁方法的执行 有两个条件 //1、当前的bean 是singleton的//2、需要关闭容器 ((ClassPathXmlApplicationContext)ac).close();}}
<?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 SomeServiceImpl myService=new SomeServiceImpl(); --> <!-- init-method 将要创建完成执行--> <!-- destroy-method 将要销毁之前执行--> <bean id="myService" class="com.gqc.ba07.SomeServiceImpl" init-method="setUp" destroy-method="tearDown"> <property name="adao" value="aaa"/> <property name="bdao" value="bbb"/> </bean> <bean class="com.gqc.ba07.MyBeanPostProcessor"/> </beans>
package com.gqc.ba07; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import sun.print.resources.serviceui; public class MyBeanPostProcessor implements BeanPostProcessor { //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; } }
package com.gqc.ba07; public interface ISomeService { void doSome(); }