spring中Bean后置处理器实现总结
BeanPostProcessor接口 bean的后置处理器
实现功能主要是 可以在bean初始化之前和之后做增强处理。
自定义MyBeanProcessor实现BeanPostProcessor接口,重写里面的postProcessBeforeInitialization和postProcessAfterInitialization方法,这里两个方法的主要是用来bean初始化(init方法)之前和之后做增强处理。
| /** * @author liujian * @date 2018/1/9 */ public class MyBeanProcessor implements BeanPostProcessor{ @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("初始化之前做处理"); //bean初始化之前做处理的方法 if(s.equals("test")){ HelloWorld helloWorld=(HelloWorld)o; helloWorld.setMessage("helloworld 处理后"); return helloWorld; } return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("初始化之后做处理"); return o; } } |
将自定义的MyBeanProcessor 添加到spring 容器中
<bean id="myBeanProcessor" class="com.spring.demo.MyBeanProcessor">
自定义一个实体类HelloWorld,并添加到spring容器中
| /** * @author liujian * @date 2018/1/8 */ public class HelloWorld { private String message; public void getMessage(){ System.out.println("message is "+message); } public void setMessage(String message){ this.message=message; } public HelloWorld() { } //初始化方法 public void init(){ System.out.println("bean 初始化方法"); } //销毁方法 public void destory(){ System.out.println("bean 销毁方法"); } } |
bean.xml文件中 配置helloworld的bean
| <?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,定义初始化方法initmethod 和销毁方法--> <bean id="test" class="com.spring.demo.HelloWorld" init-method="init" destroy-method="destory"> <property name="message" value="hello world 1111"></property> </bean> <!--自定义bean的后置处理器--> <bean class="com.spring.demo.MyBeanProcessor"/> </beans> |
测试工具类:
| /** * @author liujian * @date 2018/1/8 */ public class MainApp { public static void main(String []args){ /* ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); HelloWorld obj=(HelloWorld) context.getBean("helloWorld"); obj.getMessage(); XmlBeanFactory xmlBeanFactory=new XmlBeanFactory(new ClassPathResource("beans.xml")); HelloWorld obj1=(HelloWorld) xmlBeanFactory.getBean("helloWorld"); obj1.getMessage();*/ AbstractApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); HelloWorld objA=(HelloWorld) context.getBean("test"); objA.getMessage(); context.registerShutdownHook(); } } |
输出结果:
|
spring中Bean后置处理器实现总结的更多相关文章
- Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
- spring学习四:Spring中的后置处理器BeanPostProcessor
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
- Spring中的后置处理器BeanPostProcessor讲解
Spring中提供了很多PostProcessor供开发者进行拓展,例如:BeanPostProcessor.BeanFactoryPostProcessor.BeanValidationPostPr ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring Bean后置处理器
本例子源于:W3CSchool,在此作记录 Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己 ...
- Spring Bean 后置处理器
Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等. 你也可以在 ...
- Spring之BeanPostProcessor(后置处理器)介绍
为了弄清楚Spring框架,我们需要分别弄清楚相关核心接口的作用,本文来介绍下BeanPostProcessor接口 BeanPostProcessor 该接口我们也叫后置处理器,作用是在Be ...
- Spring 如何保证后置处理器的执行顺序 - OrderComparator
Spring 如何保证后置处理器的执行顺序 - OrderComparator Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.htm ...
- Spring-Spring Bean后置处理器
Spring Bean后置处理器 BeanPostProcessor接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等.你也可以在Spring容器通过插入一个或多个BeanPos ...
随机推荐
- java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230
近期遇到了如标题这种错误,再次记录解决方法.本文參考帖子: http://bbs.csdn.net/topics/390196217 出现此bug的原因是在内存回收上.里面用Bitamp的代码为: t ...
- 王立平--WebView的缓存机制
WebView的缓存能够分为页面缓存和数据缓存. 1. 页面缓存是指载入一个网页时的html.JS.CSS等页面或者资源数据. 这些缓存资源是因为浏览器的行为而产生.开发人员仅仅能通过配置HTTP ...
- redis参数配置说明
参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no2. 当Redis以守护进程方式运行 ...
- jersey实现文件下载
好久没有更新博客了,今天来再次总结一下,之前有整理过关于jersey实现文件上传功能的相关知识,但是前一阵子在实习过程中写接口又要实现文件下载的功能,由于这些东西基本都是使用jersey提供的注解和接 ...
- java中模拟http(https)请求的工具类
在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...
- java宜立方商城项目
宜立方商城项目 链接: https://pan.baidu.com/s/1c1SokzI 密码: z5cy 网上买的,资源是拿来共享,而不是来牟利的 框架:spring 设计模式 ...
- shell编写mysql抽取数据脚本
#!/bin/bash DT=`date +%Y%m%d` #当前日期YESTERDAY=`date -d "yesterday" +%Y-%m-%d` #昨天,用于处理数据的日期 ...
- 【python】函数filter、map
- 简述ES6其他的东西
第一是修饰器是ES7的一个提案,现在Babel转码器已经支持.那么什么是修饰器呢,修饰器是对类的行为的改变,在代码编译时发生的,而不是在运行时发生的且修饰器只能用于类和类的方法.修饰器可以接受三个函数 ...
- 安装cocoa pods时出现Operation not permitted - /usr/bin/xcodeproj的问题
安装cocoa pods时, 在命令行中输入: 安装:sudo gem install cocoapods报Operation not permitted - /usr/bin/xcodeproj这个 ...