javascript
Spring 之常用接口
?? 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 之常用接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java-常用的类
- 下一篇: 在script所在位置插入内容