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)的更多相关文章

  1. 在Spring Bean的生命周期中各方法的执行顺序

    Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种: 通过实现 InitializingBe ...

  2. 4.BeanPostProcessor 后处理Bean

     Bean种类 普通bean:之前操作的都是普通bean.<bean id="" class="A"> ,spring直接创建A实例,并返回 Fac ...

  3. spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理

    1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...

  4. iOS对UIViewController生命周期和属性方法的解析

    目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...

  5. 【iOS开发】iOS对UIViewController生命周期和属性方法的解析

    iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...

  6. vue 源码详解(二): 组件生命周期初始化、事件系统初始化

    vue 源码详解(二): 组件生命周期初始化.事件系统初始化 上一篇文章 生成 Vue 实例前的准备工作 讲解了实例化前的准备工作, 接下来我们继续看, 我们调用 new Vue() 的时候, 其内部 ...

  7. (转)Servlet的生命周期——初始化、运行、销毁全部过程

    背景:面试中很基础的一个问题,所以有必要好好整理一番. Servlet体系结构是建立在 Java 多线程机制上的,它的生命周期由 Web 容器负责. 当客户端第一次请求某个 Servlet 时,Ser ...

  8. Fragment 的生命周期及使用方法详解

    Fragment 的基础知识介绍 1.1 概述 1.1.1 特性 By hebang32624 Fragment 是 activity 的界面中的一部分或一种行为.可以把多个 Fragment 组合到 ...

  9. 一起学习vue源码 - Vue2.x的生命周期(初始化阶段)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...

随机推荐

  1. 区块链入门到实战(22)之以太坊(Ethereum) – 账号(地址)

    作用: 外部账号 – 用户使用的账号,账户余额. 合约账号 – 智能合约使用的账号,每个智能合约都有一个账号,内存和账户余额 以太坊(Ethereum)网络中,有2种账号: 外部账号 – 用户使用的账 ...

  2. jdk 功能变化

    JDK的变化 JDK1.5 JDK1.6 jdk1.7 1. 添加自动装箱,拆箱   1. =后可以省略泛型, 见ArrayList 2. try后加一个(), 定义流对象,作用域在try里 jdk1 ...

  3. OpenSIPS 2.4.2 高并发下,日志丢失怎么办

      问题年年有,今年特别多.最近公司对呼叫中心平台做了大幅度重构,基于OpenSIPS实现的会话管理服务,在高并发压测过程中,发现OpenSIPS的日志居然出现丢失的情况,简直让我食不知味,困惑不已. ...

  4. 用aop去解决事物问题(tx)记录学习之aop1.2

    上一个文章我们了解了什么事aop,以及aop的使用方法,主要是把自己想要加入的通知(advice)加入到我们的方法里, 比如上一章我们说的事把myadvice类中的before方法织入到userser ...

  5. printf size_t warning

    printf("print discoverList.size()=[%u]\n", discoverList.size()); src/ResultToDB.cpp:2768: ...

  6. JS数组遍历的十二种方式

    遍历有如下几种方式 数组方法 map forEach filter find findIndex every some reduce reduceRight 其他方法 for for in for o ...

  7. Java/后端学习路线

    点赞再看,养成习惯,微信搜一搜[三太子敖丙]关注这个喜欢写情怀的程序员. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系 ...

  8. 【Android】AndroidStudio关于EventBus报错解决方法its super classes have no public methods with the @Subscribe

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先说明,以前我用eventBus的 ...

  9. mac:app已损坏,打不开。你应该将它移到废纸篓。

    app已损坏,打不开.你应该将它移到废纸篓. http://bbs.feng.com/read-htm-tid-11230947.html http://www.codesec.net/view/50 ...

  10. ract-native常用命令

    1.新建项目:react-native init AwesomeProject 2.运动项目 cd AwesomeProject react-native run-ios 3.添加第三方插件: yar ...