当前位置: 首页 > news >正文

Spring容器生命周期中如前置运行程序和后置运行程序

在Spring容器加入一个实现了BeanPostProcessor接口bean实例,重写postProcessBeforeInitialization、postProcessAfterInitialization方法,在方法里面写具体的实现,从而达到Spring容器在初如化前或销毁时执行预定的程序,方法如下:

1、pop.xml导包
 

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.37</version>
</dependency>
<dependency>

2、写一个类实现BeanPostProcessor接口,同时写一个User实体类用于测试
实现类:

package test.spring.model;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class LifeBeanAware implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// TODO Auto-generated method stubSystem.out.println("前置处理");return null;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// TODO Auto-generated method stubSystem.out.println("后置处理");return null;}}

User类:

/**
*Description:
*author: ljd
*@date 2024年7月2日 
*@version 1.0 
*/
package test.spring.model;public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {System.out.println("user赋值");this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void testPrint() {System.out.println("this is test init function");}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + "]";}public User() {super();}}

3、applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user1" init-method="testPrint" destroy-method="testPrint" class="test.spring.model.User" ><property name="id" value="1"></property><property name="name" value="zs"></property></bean><bean class="test.spring.model.LifeBeanAware"></bean>
</beans>


4、测试结果

/**
*Description:
*author: ljd
*@date 2024年7月2日 
*@version 1.0 
*/
package testSpring;import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import test.spring.model.User;public class TestSpring {@Testpublic void testUser() {ConfigurableApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");User user=ac.getBean(User.class); System.out.println(user);ac.close();}}

http://www.lryc.cn/news/390935.html

相关文章:

  • C++ 现代教程二
  • JavaScript函数闭包解析
  • STM32MP135裸机编程:使用软件触发硬件复位
  • 【饼图交通方式】用ECharts的graphic配置打造个性化
  • 大模型学习笔记3【大模型】LLaMA学习笔记
  • 工程师 - 什么是SMP
  • Webpack: 并行构建
  • Vue的介绍与使用
  • MYSQL双主双从,使用Keepalived双机热备+LVS高可用群集
  • 9.计算机视觉—目标检测
  • 构造函数深入理解
  • Rocky Linux 9 快速安装docker 教程
  • go语言并发编程1-Gouroutine
  • Sylar服务器框架——Http模块
  • 7km远距离WiFi实时图传模块,无人机海上无线传输方案,飞睿智能WiFi MESH自组网技术
  • 2024年上半年网络工程师下午真题及答案解析
  • Jmeter下载、安装及配置
  • 掌握高效实用的VS调试技巧
  • 实验2 字符及字符串输入输出与分支程序设计实验
  • docker容器间网络仿真工具-pumba
  • A36 STM32_HAL库函数 之PCD通用驱动 -- B -- 所有函数的介绍及使用
  • vue2 + element三级菜单实现模板
  • vue H5页面video 视频流自动播放, 解决ios不能自动播放问题
  • 自闭症儿童:探索症状背后的多彩内心世界
  • 在Centos7上安装PostgreSQL16的详细步骤
  • MySQL 图形化界面
  • 【人工智能】GPT-5的即将到来:从高中生进化到,,,博士生?
  • 【收录率高丨投稿范围广 | 往届均已EI检索】第四届光学与通信技术国际学术会议(ICOCT 2024,8月9-11)
  • 小阿轩yx-LVS负载均衡群集
  • CPP知识点记录总结