生命周期(初始化、销毁方法、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 ...
随机推荐
- Datanode 怎么与 Namenode 通信?
在分析DataNode时, 因为DataNode上保存的是数据块, 因此DataNode主要是对数据块进行操作. A. DataNode的主要工作流程 客户端和DataNode的通信: 客户端向Dat ...
- 如何简洁优雅地部署PostgreSQL和Pgweb?
本文转自Rancher Labs 介绍PostgreSQL和Pgweb PostgreSQL是一款以可靠性和性能为人所熟知的开源数据库.它在很多行业和应用程序中都有应用,尤其是web开发人员的最爱.从 ...
- asp.net报表结构学习记录
当一份web报表项目压缩包躺在我的文件夹里时,我是完全懵的.作为一个学习了一个月java的asp.net小白,以前从来没有接触过这方面,我完全不知道从何入手. 手里也有asp.net开发学习视频,但都 ...
- Jenkins匿名用户设置
最近自己安装配置jenkins,但是跑任务时,发现是匿名账户登录,可以在系统设置中点击如下: 2.勾选“启用安全”,“访问控制”>“安全域”选择“Jenkins专有用户数据库”,并勾选“允许用户 ...
- 《HelloGitHub》第 53 期
兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...
- Java面试题(Mybatis篇)
Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? #{}是预编译处理,${}是字符串替换: Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用Prepared ...
- C++ strcmp与strncmp的比较
C++ strcmp与strncmp的比较(转载) 原文链接:https://www.cnblogs.com/ybqjymy/p/12565444.html strcmp与strncmp都是用来比较 ...
- [PyTorch 学习笔记] 3.3 池化层、线性层和激活函数层
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson3/nn_layers_others.py 这篇文章主要介绍 ...
- 递归 & 分治算法深度理解
首先简单阐述一下递归,分治算法,动态规划,贪心算法这几个东西的区别和联系,心里有个印象就好. 递归是一种编程技巧,一种解决问题的思维方式:分治算法和动态规划很大程度上是递归思想基础上的(虽然实现动态规 ...
- Spring Validation-用注解代替代码参数校验
Spring Validation 概念 在原先的编码中,我们如果要验证前端传递的参数,一般是在接受到传递过来的参数后,手动在代码中做 if-else 判断,这种编码方式会带来大量冗余代码,十分的不优 ...