1、初始化和销毁
在目标方法执行前后进行初始化或销毁
(1)在Service方法的实现类里面创建初始化方法和销毁方法:
public class StudentServiceImpl implements StudentService { private StudentDao studentDao; public StudentServiceImpl() { System.out.println("service的实现类被创建了!!"); } public StudentDao getStudentDao() { return studentDao; } public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public void addStudent(){ System.out.println("StudentService的实现类的Add方法!!"); studentDao.addStudent(); } public void myInit(){ System.out.println("初始化方法"); } public void myDestory(){ System.out.println("销毁方法"); } }
(2)配置文件中对初始化方法和销毁方法进行配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <bean id="studentDao" class="pers.zhb.dao.StudentDaoImpl"> </bean> <bean id="studentService" class="pers.zhb.service.StudentServiceImpl" init-method="myInit" destroy-method="myDestory"> <property name="studentDao" ref="studentDao"></property> </bean> </beans>
(3)测试:
public class TestCycle { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象 StudentService studentService= (StudentService) applicationContext.getBean("studentService"); studentService.addStudent(); applicationContext.getClass().getMethod("close").invoke(applicationContext); } }
必须执行close方法(这里是利用反射调用的close()方法)后销毁方法才会执行,必须是单例的。
(4)作用:
初始化方法:用于准备数据等
销毁方法:释放资源等
2、BeanPostProcessor后处理Bean
spring提供一种机制,只要实现了BeanPostProcessor接口,并将实现类提供给spring容器,spring容器将会自动执行,在初始化方法前执行before(),初始化方法后执行after()
spring提供工厂勾子,用于修改实例对象,可以生成代理对象,是AOP的底层
(1)创建MyBeanPostProcessor类,实现 BeanPostProcessor 接口:
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("前方法"+s); return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("后方法"+s); return o; } }
(2)配置文件:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <bean id="studentDao" class="pers.zhb.dao.StudentDaoImpl"> </bean> <bean id="studentService" class="pers.zhb.service.StudentServiceImpl" init-method="myInit" destroy-method="myDestory"> <property name="studentDao" ref="studentDao"></property> </bean> <bean class="pers.zhb.abc.MyBeanPostProcessor"></bean> </beans>
(3)测试:
public class TestCycle { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象 StudentService studentService= (StudentService) applicationContext.getBean("studentService"); studentService.addStudent(); applicationContext.getClass().getMethod("close").invoke(applicationContext); } }
前方法studentDao 后方法studentDao service的实现类被创建了!! 前方法studentService 初始化方法 后方法studentService StudentService的实现类的Add方法!! StudentDao的实现类的Add方法!! 四月 13, 2020 3:54:12 下午 org.springframework.context.support.AbstractApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3f91beef: startup date [Mon Apr 13 15:54:11 CST 2020]; root of context hierarchy 销毁方法
(4)动态代理的方式:
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("前方法"+s); return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("后方法"+s); return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("开启事务"); //执行目标方法 Object o1=method.invoke(o,args); System.out.println("提交事务"); return o1; } }); } }
前方法studentDao 后方法studentDao service的实现类被创建了!! 前方法studentService 初始化方法 后方法studentService 开启事务 StudentService的实现类的Add方法!! 开启事务 StudentDao的实现类的Add方法!! 提交事务 提交事务 四月 13, 2020 4:09:20 下午 org.springframework.context.support.AbstractApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3f91beef: startup date
[Mon Apr 13 16:09:20 CST 2020]; root of context hierarchy 销毁方法