生命周期(初始化、销毁方法、BeanPostProcessor后处理Bean)
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
销毁方法
生命周期(初始化、销毁方法、BeanPostProcessor后处理Bean)的更多相关文章
- 在Spring Bean的生命周期中各方法的执行顺序
Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种: 通过实现 InitializingBe ...
- 4.BeanPostProcessor 后处理Bean
Bean种类 普通bean:之前操作的都是普通bean.<bean id="" class="A"> ,spring直接创建A实例,并返回 Fac ...
- spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理
1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...
- iOS对UIViewController生命周期和属性方法的解析
目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...
- 【iOS开发】iOS对UIViewController生命周期和属性方法的解析
iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...
- vue 源码详解(二): 组件生命周期初始化、事件系统初始化
vue 源码详解(二): 组件生命周期初始化.事件系统初始化 上一篇文章 生成 Vue 实例前的准备工作 讲解了实例化前的准备工作, 接下来我们继续看, 我们调用 new Vue() 的时候, 其内部 ...
- (转)Servlet的生命周期——初始化、运行、销毁全部过程
背景:面试中很基础的一个问题,所以有必要好好整理一番. Servlet体系结构是建立在 Java 多线程机制上的,它的生命周期由 Web 容器负责. 当客户端第一次请求某个 Servlet 时,Ser ...
- Fragment 的生命周期及使用方法详解
Fragment 的基础知识介绍 1.1 概述 1.1.1 特性 By hebang32624 Fragment 是 activity 的界面中的一部分或一种行为.可以把多个 Fragment 组合到 ...
- 一起学习vue源码 - Vue2.x的生命周期(初始化阶段)
作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...
随机推荐
- Combining STDP and Reward-Modulated STDP in Deep Convolutional Spiking Neural Networks for Digit Recognition
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract 灵长类视觉系统激发了深度人工神经网络的发展,使计算机视觉领域发生了革命性的变化.然而,这些网络的能量效率比它们的生物学对 ...
- python - 常用数据清洗方法-重复项处理
在数据的处理过程中,一般都需要进行数据清洗工作,如数据集是否存在重复,是否存在缺失,数据是否具有完整性和一致性,数据中是否存在异常值等.发现诸如此类的问题都需要针对性地处理,下面我们一起学习常用的数据 ...
- 区块链入门到实战(34)之Solidity – 变量
Solidity 支持三种类型的变量: 状态变量 – 变量值永久保存在合约存储空间中的变量. 局部变量 – 变量值仅在函数执行过程中有效的变量,函数退出后,变量无效. 全局变量 – 保存在全局命名空间 ...
- go语言之抛出异常
一: panic和recover 作用:panic 用来主动抛出错误: recover 用来捕获 panic 抛出的错误. 概述: ,引发panic有两种情况 )程序主动调用panic函数 )程序产生 ...
- jsp页面关于isELIgnored="false",页面无法解析数据问题
问题: 首先确定所取的集合里面是否有值,如果没有先检查集合 如果有,就再jsp页面头部添加: isELIgnored="false" 具体如下: <%@ page langu ...
- oeasy教您玩转linux010202软件包管理apt
顾一下 上一部分我们都讲了什么?
- Tesselation学习
Tesselation的作用:给低片面数模型镶嵌更多片面,让低模变高模. 和法线贴图不同,法线本质是通过改变低模表面的颜色来模拟高模,比如在一个片面上普通diffuse是均匀的颜色分布(因为光照颜色一 ...
- JVM内存区域以及各区域的内存溢出异常,内存分代策略,垃圾收集算法,各种垃圾收集器
本文整理自周志明老师的<深入理解Java虚拟机-JVM高级特性与最佳实践>第3版的第二章和第三章. 加上了一些网上拼拼凑凑的图片,个人认为很多博客复制来复制去,最后的东西都看不懂,所以从书 ...
- java初探(1)之防止库存为负以及防超买
在秒杀业务中,会出现当只剩一个库存时,但有多个人仍然秒杀成功,且都减库存成功,因此,在减库存,更新数据库的时候,需要在sql语句上进行判断,是否库存大于0. @Update("update ...
- hdu6704 2019CCPC网络选拔赛1003 K-th occurrence 后缀自动机+线段树合并
解题思路: fail树上用权值线段树合并求right/endpos集合,再用倍增找到待查询串对应节点,然后权值线段树求第k大. #include<bits/stdc++.h> using ...