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. 快醒醒,C# 9 中又来了一堆关键词 init,record,with

    一:背景 1. 讲故事 .NET5 终于在 2020-08-25 也就是前天发布了第八个预览版,这么多的预览版搞得我都麻木了,接踵而来的就是更多的新特性加入到了 C# 9 中,既然还想呆在这条船上,得 ...

  2. 新手学习seo写原创文章的方法

    http://www.wocaoseo.com/thread-102-1-1.html 前两天都是写自己公司克拉玛依电信公司年终活动和总结的事,今天继续学习seo技术,其实说难也难说容易也容易,关键的 ...

  3. caoz:数据分析这点事

    http://www.wocaoseo.com/thread-53-1-1.html 先声明一下,按照传统的定义,我还真不是数据分析高手,各种关联算法,只会最简单的一种(话说不少场合还算管用):各种挖 ...

  4. 单元测试与单元测试框架 Jest

    什么是单元测试? 测试是一种验证我们的代码是否可以按预期工作的手段. 被测试的对象可以是我们程序的任何一个组成部分.大到一个分为多步骤的下单流程,小到代码中的一个函数. 单元测试特指被测试对象为程序中 ...

  5. 使用kind快速创建本地集群

    简 介 kind是另一个Kubernetes SIG项目,但它与minikube有很大区别.它可以将集群迁移到Docker容器中,这与生成虚拟机相比,启动速度大大加快.简而言之,kind是一个使用Do ...

  6. Android开发,java开发程序员常见基础面试题,更换两个变量的值,java逻辑代码

    public class abswitch{ public static void main (String args []){ //author:qq986945193 int a=1; int b ...

  7. Unity资源引用问题

    前几天做项目时,遇到一个奇怪的问题: 从一处复制了一个预制体,预制体上面还附有一个材质球,材质球上关联着另一张贴图. 将所有关联的东西,均Copy两份,然后关联成一个新的预制体,最后用项目内的读取内存 ...

  8. 【python】我OUT了,原来函数中的冒号和箭头是这么回事

    翻了翻httprunner的源代码,越看越不对劲,感觉有点看不懂语法了. 这都什么鬼?感觉心好慌,顿时感到惭愧万分,还好意思说自己了解Python呢. 赶紧了解一下,原来这叫 type hints,是 ...

  9. Java 中基本数据类型的变量的转换规则

    基本数据类型之间的转换 变量之间的转换规则 布尔型变量在和其他 7 种基本数据类型做运算时,无法转化为其他的数据类型,所以下面所说的运算都是除了布尔型的其他 7 种基本数据类型之间的转换. 1.自动类 ...

  10. graph attention network(ICLR2018)官方代码详解(te4nsorflow)

    论文地址:https://arxiv.org/abs/1710.10903 代码地址: https://github.com/Diego999/pyGAT 我并没有完整看过这篇论文,但是在大致了解其原 ...