网站建设合同要不要交印花税,广州安卓程序开发,怎么做废品收购网站,网站制作手机端Spring后处理器-BeanPostProcessor
Bean被实例化后#xff0c;到最终缓存到名为singletonObjects单例池之前#xff0c;中间会经过bean的初始化过程#xff08;#xff08;该后处理器的执行时机#xff09;#xff09;#xff0c;例如#xff1a;属性的填充、初始化方…Spring后处理器-BeanPostProcessor
Bean被实例化后到最终缓存到名为singletonObjects单例池之前中间会经过bean的初始化过程该后处理器的执行时机例如属性的填充、初始化方法init的执行等其中有一个对外拓展的点BeanPostProcessor我们称之为bean后处理器。与上文bean工厂后处理器相似它也是一个接口实现了该接口并被容器管理的BeanPostProcessor即在配置文件中对其进行配置会在流程节点上被Spring自动调用。BeanPostProcessor接口代码如下 //
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;public interface BeanPostProcessor {Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}创建实现该接口BeanPsotProcessor的类要在配置文件中进行管理 快捷键 ctrl insert 重写接口方法 package com.example.PostProcessor;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName :postProcessBeforeInitialization);return bean;}Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName :postProcessAfterInitialization);return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);}
}测试类代码 package com.example.Test;import com.example.Service.Impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApplicationContext {public static void main(String[] args) {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(application.xml);System.out.println(context.getBean(UserServiceImpl.class));}
}运行结果如下 展示了该后处理器的执行时机 Before和After执行时机
在Bean实例化过程可以配置相关对于bean对象的操作方法具体间往期文章Bean的配置- CSDN搜索注册为bean的类 package com.example.Service.Impl;import com.example.DAO.UserDAO;
import com.example.Service.UserService;
import org.springframework.beans.factory.InitializingBean;import java.util.List;
import java.util.Map;
import java.util.Set;public class UserServiceImpl implements UserService, InitializingBean {// todo 无参构造方法public UserServiceImpl() {System.out.println(UserServiceImpl实例化);}// todo 自定义初始化方法public void init() {System.out.println(自定义初始化方法init());}Overridepublic void afterPropertiesSet() throws Exception {System.out.println(属性设置之后执行afterPropertiesSet());}}实现bean后处理器的类 package com.example.PostProcessor;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName :postProcessBeforeInitialization);return bean;}Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName :postProcessAfterInitialization);return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);}
}配置文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:utilhttp://www.springframework.org/schema/utilxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdbean classcom.example.PostProcessor.MyBeanPostProcessor/beanbean iduserService classcom.example.Service.Impl.UserServiceImpl init-methodinit
/beans 测试类 package com.example.Test;import com.example.Service.Impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApplicationContext {public static void main(String[] args) {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(application.xml);System.out.println(context.getBean(UserServiceImpl.class));}
}运行结果
小结
从上述运行结果来看首先完成bean对象的创建然后执行后处理器中的before方法然后执行属性设置之后的方法然后执行自定义的初始化方法最后执行后处理器的after方法 案例
对Bean方法进行执行时间日志增强要求 Bean的方法执行之前控制台打印当前时间Bean的方法执行之后控制台打印当前时间分析 对方法进行增强主要就是代理设计模式和包装设计模式由于Bean方法不确定所以使用动态代理在运行期间执行增强操作在Bean实例创建完毕之后进入到单例之前使用Proxy真实的目标bean 文章转载自: http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.nckjk.cn.gov.cn.nckjk.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.mnnxt.cn.gov.cn.mnnxt.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.bqfpm.cn.gov.cn.bqfpm.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.kndst.cn.gov.cn.kndst.cn