生命周期(初始化、销毁方法、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 ...
随机推荐
- Java GUI 图书管理系统
01 概述 一款功能强大的图书馆管理系统,功能齐全,小白/大学生项目实训,学习的不二之选. 02 技术 此系统使用 java awt 实现.java.awt是一个软件包,包含用于创建用户界面和绘制图形 ...
- python实现对列表的增删查修操作
#定义一个空列表 list_demo=[] #1,向列表中插入元素 def append_demo(): #第一种使用append,可以在列表末尾添加一个函数 for i in range(2): l ...
- 传统servelt项目之分页操作
需求说明: • 演示最终分页效果 • 提供分页素材 • 分页的作用 • 数据量大,一页容不下 • 后台查询部分数据而不是全部数据 • 降低带宽使用,提高访问速度 • 分页的实现思路 • ...
- 终于有架构师用401页PDF+194张图表把设计模式讲明白了
十几年来,IT 界也发生了翻天覆地的变化,各种开源框架层出不穷,机器学习大兴其道.但是,在面向对象编程中,设计模式的重要性却不曾改变.与以前一样,在大规模的企业系统开发中,Java和C#仍处于主导地位 ...
- python爬虫实战---爬取大众点评评论
python爬虫实战—爬取大众点评评论(加密字体) 1.首先打开一个店铺找到评论 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经 ...
- MD5加密,java String 转变成MD5 String 详细代码,工具类Android开发必备
/** * MD5加码.32位 * @param inStr * @return */ public static String MD5(String inStr) { MessageDigest m ...
- 关于action的使用在firefox报错的问题
现在的网站有很多都是鼠标移到上面去才会显示出相应的一些标签之类的东西,然后再进行操作,但是因为要操作的元素一开始是隐藏的,就没办法直接定位,只能调用action来模拟鼠标悬停操作,也就是下面这句代码: ...
- 网站远程附件存储到 OSS
参考:链接 链接 链接 简介 网站远程附件功能是指将用户上传的附件直接存储到远端的存储服务器,一般是通过FTP的方式存储到远程的FTP服务器,将论坛附件保存在 OSS 上有以下好处: 附件将拥有更 ...
- 使用zabbix监控sql server的发布订阅
(一)背景 个人在使用sql server时,用到了sql server的发布订阅来做主从同步,类似MySQL的异步复制.在发布订阅环境搭建完成后,最重要的就是如何监控复制的状态了,sql serve ...
- Agumaster 增加日交易数据列表