1.ApplicationContextAware

任何期望在ApplicationContext运行的时候被通知到都可以实现该接口

/*** 测试Spring ApplicationContextAware接口* @author zhangwei_david* @version $Id: TestApplicationContextAware.java, v 0.1 2015年1月3日 上午11:42:19 zhangwei_david Exp $*/
@Component
public class TestApplicationContextAware implements ApplicationContextAware {/*** @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)*/public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {LoggerUtils.info("ApplicationContext runs in");}}

  

启动过程的日志如下:

一月 03, 2015 11:46:02 上午 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'executor' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
一月 03, 2015 11:46:02 上午 org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize
信息: Initializing ExecutorService  'scheduler'
一月 03, 2015 11:46:02 上午 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'scheduler' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
一月 03, 2015 11:46:02 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132c619: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,app,asyncDemo,testClient,testApplicationContextAware,executor,scheduler,org.springframework.context.annotation.internalAsyncAnnotationProcessor,org.springframework.context.annotation.internalScheduledAnnotationProcessor,studentBiz,beforeAdvice,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,afterAdvice,helloWord,fileCopier,mbeanExporter,assembler,messageSource,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
11:46:02.886 [main] INFO  com.cathy.demo.util.LoggerUtils - ApplicationContext runs in
一月 03, 2015 11:46:02 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.apache.cxf.bus.spring.BusApplicationContext@121202d: startup date [Sat Jan 03 11:46:02 CST 2015]; root of context hierarchy
一月 03, 2015 11:46:03 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
一月 03, 2015 11:46:03 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-extension-jaxws.xml]
一月 03, 2015 11:46:03 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-extension-soap.xml]
一月 03, 2015 11:46:03 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

  

2 BeanNameAware

如果Bean想知道在BeanFactory中设置的名字时可以实现该接口

/*** 测试BeanNameAware接口* @author zhangwei_david* @version $Id: TestBeanNameAware.java, v 0.1 2015年1月3日 上午11:48:50 zhangwei_david Exp $*/
@Component(value = "hello")
public class TestBeanNameAware implements BeanNameAware {private String beanName;/*** @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)*/public void setBeanName(String name) {LoggerUtils.info("setBeanName(" + name + ")");beanName = name;}/*** Getter method for property <tt>beanName</tt>.** @return property value of beanName*/public String getBeanName() {return beanName;}}

  

结果是:

12:10:47.496 [main] INFO  com.cathy.demo.util.LoggerUtils - setBeanName(hello)

3. InitializingBean

如果期望在BeanFactory 设置所有的属性后作出进一步的反应可以实现该接口

/*** 测试InitializingBean* @author zhangwei_david* @version $Id: TestInitializingBean.java, v 0.1 2015年1月3日 下午12:04:38 zhangwei_david Exp $*/
@Component()
public class TestInitializingBean implements InitializingBean, BeanNameAware {private String beanName;/*** @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()*/public void afterPropertiesSet() throws Exception {LoggerUtils.info("Bean的属性都被设置完成:" + beanName);}/*** @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)*/public void setBeanName(String name) {beanName = name;}}

  

结果是:

12:10:47.496 [main] INFO  com.cathy.demo.util.LoggerUtils - Bean的属性都被设置完成:testInitializingBean

  

在Spring中有两种方式在Bean的全部属性都设置成功后执行特定的行为,除了实现InitializingBean接口外还可以在Spring的配置文件中指定init-method属性。那么如果这两者同时存在执行的属性又该是什么样的呢?

/***  测试InitializingBean接口的特点** @author zhangwei_david* @version $Id: MyInitTest.java, v 0.1 2015年6月7日 下午5:46:27 zhangwei_david Exp $*/public class MyInitTest implements InitializingBean {public MyInitTest() {System.out.println("------------MyInitTest 构造方法被调用-------------");}public void init() {System.out.println("------------spring 配置的initMethod 被调用-------------");}/*** @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()*/public void afterPropertiesSet() throws Exception {System.out.println("------------InitializingBean.afterPropertiesSet ()方法被调用-------------");}}<bean id="myInitTest" class="com.cathy.demo.spring.MyInitTest"init-method="init" />

  

2015-06-07 17:56:47  [ main:0 ] - [ INFO ]  @TestExecutionListeners is not present for class [class com.cathy.demo.spring.MySpringTest]: using defaults.
2015-06-07 17:56:47  [ main:145 ] - [ INFO ]  Loading XML bean definitions from URL [file:/H:/Alipay.com/workspace4alipay/demo/target/classes/META-INF/spring/test-beans.xml]
2015-06-07 17:56:47  [ main:327 ] - [ INFO ]  JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2015-06-07 17:56:47  [ main:359 ] - [ INFO ]  Refreshing org.springframework.context.support.GenericApplicationContext@15f7ae5: startup date [Sun Jun 07 17:56:47 CST 2015]; root of context hierarchy
2015-06-07 17:56:47  [ main:472 ] - [ INFO ]  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2db087: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myInitTest,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
------------MyInitTest 构造方法被调用-------------
------------InitializingBean.afterPropertiesSet ()方法被调用-------------
------------spring 配置的initMethod 被调用-------------
2015-06-07 17:56:47  [ Thread-0:493 ] - [ INFO ]  Closing org.springframework.context.support.GenericApplicationContext@15f7ae5: startup date [Sun Jun 07 17:56:47 CST 2015]; root of context hierarchy
2015-06-07 17:56:47  [ Thread-0:494 ] - [ INFO ]  Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2db087: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myInitTest,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy我们从日志可以可以发现,它们的执行顺序是, afterPropertiesSet()->initMethod()

  

 

在使用注解的方式指定initMehthod的方式是在initMethod()上添加 @PostConstruct 此时的执行顺序是否还是这样呢?我们看看下面的测试,Spring的配置文件中不在有bean的配置

/***  测试InitializingBean接口的特点** @author zhangwei_david* @version $Id: MyInitTest.java, v 0.1 2015年6月7日 下午5:46:27 zhangwei_david Exp $*/
@Component
public class MyInitTest implements InitializingBean {public MyInitTest() {System.out.println("------------MyInitTest 构造方法被调用-------------");}@PostConstructpublic void init() {System.out.println("------------spring 配置的initMethod 被调用-------------");}/*** @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()*/public void afterPropertiesSet() throws Exception {System.out.println("------------InitializingBean.afterPropertiesSet ()方法被调用-------------");}}

  

结果是:

2015-06-07 18:09:44  [ main:0 ] - [ INFO ]  @TestExecutionListeners is not present for class [class com.cathy.demo.spring.MySpringTest]: using defaults.
2015-06-07 18:09:44  [ main:136 ] - [ INFO ]  Loading XML bean definitions from URL [file:/H:/Alipay.com/workspace4alipay/demo/target/classes/META-INF/spring/test-beans.xml]
2015-06-07 18:09:44  [ main:311 ] - [ INFO ]  JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2015-06-07 18:09:44  [ main:346 ] - [ INFO ]  Refreshing org.springframework.context.support.GenericApplicationContext@15f7ae5: startup date [Sun Jun 07 18:09:44 CST 2015]; root of context hierarchy
2015-06-07 18:09:44  [ main:480 ] - [ INFO ]  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1fe3806: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myInitTest,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
------------MyInitTest 构造方法被调用-------------
------------spring 配置的initMethod 被调用-------------
------------InitializingBean.afterPropertiesSet ()方法被调用-------------
2015-06-07 18:09:44  [ Thread-0:500 ] - [ INFO ]  Closing org.springframework.context.support.GenericApplicationContext@15f7ae5: startup date [Sun Jun 07 18:09:44 CST 2015]; root of context hierarchy
2015-06-07 18:09:44  [ Thread-0:501 ] - [ INFO ]  Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1fe3806: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myInitTest,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

  

4. BeanPostProcessor

BeanPostProcessor 是BeanFactory的钩子允许客户对新建的Bean进行修改

/*** Test BeanPostProcessor* @author zhangwei_david* @version $Id: TestBeanPostProcessor.java, v 0.1 2015年1月3日 下午12:14:32 zhangwei_david Exp $*/
@Component
public class TestBeanPostProcessor implements BeanPostProcessor {/*** @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)*/public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {LoggerUtils.info("bean初始化之前调用:bean=" + bean + ", beanName" + beanName);return bean;}/*** @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)*/public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {LoggerUtils.info("bean初始化之后调用:bean=" + bean + ", beanName" + beanName);return bean;}}

  

 结果是:
2:16:06.314 [main] INFO  com.cathy.demo.util.LoggerUtils - ApplicationContext runs in
12:16:06.314 [main] INFO  com.cathy.demo.util.LoggerUtils - bean初始化之前调用:bean=com.cathy.demo.test.TestApplicationContextAware@f70944, beanNametestApplicationContextAware
after test for AOP   postProcessBeforeInitialization
12:16:06.330 [main] INFO  com.cathy.demo.util.LoggerUtils - bean初始化之后调用:bean=com.cathy.demo.test.TestApplicationContextAware@f70944, beanNametestApplicationContextAware
after test for AOP   postProcessAfterInitialization
12:16:06.330 [main] INFO  com.cathy.demo.util.LoggerUtils - setBeanName(hello)
12:16:06.330 [main] INFO  com.cathy.demo.util.LoggerUtils - bean初始化之前调用:bean=com.cathy.demo.test.TestBeanNameAware@a6bea6, beanNamehello
after test for AOP   postProcessBeforeInitialization
12:16:06.330 [main] INFO  com.cathy.demo.util.LoggerUtils - bean初始化之后调用:bean=com.cathy.demo.test.TestBeanNameAware@a6bea6, beanNamehello
after test for AOP   postProcessAfterInitialization
12:16:06.330 [main] INFO  com.cathy.demo.util.LoggerUtils - bean初始化之前调用:bean=com.cathy.demo.test.TestInitializingBean@1b21bd3, beanNametestInitializingBean
after test for AOP   postProcessBeforeInitialization
12:16:06.330 [main] INFO  com.cathy.demo.util.LoggerUtils - Bean的属性都被设置完成:testInitializingBean
12:16:06.346 [main] INFO  com.cathy.demo.util.LoggerUtils - bean初始化之后调用:bean=com.cathy.demo.test.TestInitializingBean@1b21bd3, beanNametestInitializingBean
after test for AOP   postProcessAfterInitialization

  

/***  测试InitializingBean接口的特点*  测试BeanPostProcessor接口** @author zhangwei_david* @version $Id: MyInitTest.java, v 0.1 2015年6月7日 下午5:46:27 zhangwei_david Exp $*/
@Component
public class MyInitTest implements InitializingBean, BeanPostProcessor {private Person man;public MyInitTest() {System.out.println("------------MyInitTest 构造方法被调用-------------");}@PostConstructpublic void init() {System.out.println("------------spring 配置的initMethod 被调用-------------");}/*** @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()*/public void afterPropertiesSet() throws Exception {System.out.println("------------InitializingBean.afterPropertiesSet ()方法被调用-------------");}/*** @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)*/public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {System.out.println("---------在" + beanName+ "初始化之前调用 postProccessBeforeInitializaiton--------");return bean;}/*** @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)*/public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {System.out.println("---------在" + beanName+ "初始化之后 调用 postProcessAfterInitialization--------");return bean;}/*** Setter method for property <tt>man</tt>.** @param man value to be assigned to property man*/@Autowiredpublic void setMan(Person man) {System.out.println(" 设置属性 man=" + man.getSex());this.man = man;}}

  

------------MyInitTest 构造方法被调用-------------
2015-06-07 18:49:55  [ main:480 ] - [ INFO ]  Bean 'man' of type [class com.cathy.demo.spring.Man] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)设置属性 man=Male
------------spring 配置的initMethod 被调用-------------
------------InitializingBean.afterPropertiesSet ()方法被调用-------------
2015-06-07 18:49:55  [ main:492 ] - [ INFO ]  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@de0926: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myInitTest,man,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
---------在com.cathy.demo.spring.MySpringTest初始化之前调用 postProccessBeforeInitializaiton--------
---------在com.cathy.demo.spring.MySpringTest初始化之后 调用 postProcessAfterInitialization--------
2015-06-07 18:49:55  [ Thread-0:506 ] - [ INFO ]  Closing org.springframework.context.support.GenericApplicationContext@115b42e: startup date [Sun Jun 07 18:49:55 CST 2015]; root of context hierarchy
2015-06-07 18:49:55  [ Thread-0:507 ] - [ INFO ]  Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@de0926: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myInitTest,man,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

  

转载于:https://www.cnblogs.com/wei-zw/p/8797804.html

Spring 之常用接口相关推荐

  1. Spring8:一些常用的Spring Bean扩展接口

    前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...

  2. springbean的生命周期_spring bean生命周期(涵盖spring常用接口的载入)

    spring bean生命周期流程图: ​ ​​​ 其中包含了很多常用的接口,可以参考spring 常用接口: 下面写个例子证明下: 1.实现InitializingBean以及各个Aware接口 p ...

  3. java 扩展接口_详解常用的Spring Bean扩展接口

    前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...

  4. Spring MVC常用注解说明

    2019独角兽企业重金招聘Python工程师标准>>>     使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02     之前在使用Struts2实现MV ...

  5. 盘点springmvc的常用接口

    2019独角兽企业重金招聘Python工程师标准>>> 盘点springmvc的常用接口### springmvc是如今非常流行的web开发框架之一.我个人非常喜欢它约定优于配置的理 ...

  6. 近100个Spring/SpringBoot常用注解汇总!

    作者 | Guide 来源 | JavaGuide(微信公众号) 毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我 ...

  7. spring boot 常用项目文件结构

    spring boot 常用项目文件结构 文件结构 文件结构 src/main/java 开发代码以及主程序入口 Application.java作为程序主入口,建议放在根目录下,主要用于一些框架配置 ...

  8. 声明式事务、Spring 中常用注解、Ajax

    五. 声明式事务 编程式事务: 1.1 由程序员编程事务控制代码. 1.2 OpenSessionInView 编程式事务 声明式事务: 先引入依赖 <dependency><gro ...

  9. 接近8000字的Spring/SpringBoot常用注解总结!安排!

    文章目录 0.前言 1. `@SpringBootApplication` 2. Spring Bean 相关 2.1. `@Autowired` 2.2. `Component`,`@Reposit ...

最新文章

  1. 重磅引才!符合条件博士生,给予300平以上别墅一幢!
  2. 一文精简介绍CNN基本结构
  3. python requests详解_python的requests模块参数详解
  4. leetcode 223. Rectangle Area | 223. 矩形面积(Java)
  5. 100道Java中高级面试题汇总+详细拆解
  6. 菜鸟学asp.net遇到的问题和解决方案
  7. numpy.outer
  8. 9. HTML DOM getElementsByName() 方法
  9. MAGIX Sound Forge Audio Studio v16.0.0.39 WiN 音频编辑软件
  10. 智鼎在线测评是测什么_为什么求职者会反感企业招聘用的人才测评?
  11. 华为手机浏览器 onclick失灵的问题
  12. CodeForces - 497D Gears
  13. 360 ie8兼容模式 网页兼容问题
  14. UE开机入网流程及RRC连接建立
  15. 集成微透镜阵列的CMOS传感器分析
  16. c语言control函数,C语言05-ControlFl.ppt
  17. 一个利用浏览器原生execCommand()方法实现的富文本编辑器
  18. 三洋服务器显示F6,格力空调故障代码f6什么原因
  19. mysql TIMESTAMP 不能为NULL
  20. seo建设者_SEO优化学习笔记

热门文章

  1. python 装饰器入门
  2. Android应用程序线程消息循环模型分析(4)
  3. 程序物语(六):探寻你的与众不同之处
  4. 为炒股每天只花3元 MM从贷款上学到掌控千万
  5. Shell中的${}、##和%%使用范例
  6. Bayesian Neural Network for regression (PRML)
  7. 【Deep Learning】Tensorflow实现线性回归
  8. Java实现算法导论中Rabin-Karp字符串匹配算法
  9. Vue——基本的代码结构和插值表达式、v-cloak||Vue指令之v-text和v-html||v-bind的三种用法||Vue指令之v-on
  10. 字节流练习:图片复制