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" />
log4j:WARN custom level class [# 输出DEBUG级别以上的日志] not found. 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 构造方法被调用-------------"); } @PostConstruct 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 ()方法被调用-------------"); } }
结果是:
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; } }
结果是:
12: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 构造方法被调用-------------"); } @PostConstruct 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 ()方法被调用-------------"); } /** * @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 */ @Autowired public 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
相关推荐
标题 "spring最常用jar包" 暗示了我们讨论的核心是Spring框架中不可或缺的库文件,这些jar包是开发人员在使用Spring进行Java应用程序开发时最常引用的基础组件。Spring是一个开源的Java平台,它提供了全面的企业级...
本篇将详细介绍标题“使用spring环境常用jar”所涵盖的关键知识点。 1. **Spring IOC(Inversion of Control)**:Spring的IOC容器负责创建对象、管理对象之间的关系,它将对象的创建和维护权交给了框架,从而降低...
在这个名为"spring常用jar包"的压缩包中,包含了一些Spring框架运行所必需的核心库。下面我们将逐一解析这些jar包的功能和重要性。 1. aspectjrt.jar:这是AspectJ运行时库,用于支持面向切面编程(AOP)。AOP是...
本教程将详细讲解如何将Web Service服务接口与Spring框架进行整合,以便在实际开发中实现高效、灵活的服务提供。 首先,让我们了解一下Web Service的基本概念。Web Service是一种软件系统,它通过使用开放标准(如...
本文将深入探讨Spring框架中常用的26个包,这些包是构建高效、可维护的Java应用的基础。 1. `org.springframework.beans`:这个包主要处理JavaBeans,包括属性的读取、设置、类型转换以及事件处理等。它还提供了...
在这个"spring3.1.1常用jar包"中,包含了一系列核心的Spring库,这些库是搭建基于Spring的应用程序所必需的。 1. **Spring Core**:这是Spring框架的基础,提供了依赖注入(DI)和面向切面编程(AOP)的核心功能。...
这个压缩包包含的“spring常用包”很可能是为了帮助开发者理解并使用 Spring 2.5 的核心组件。让我们详细探讨一下 Spring 2.5 中的一些关键知识点: 1. **IoC(Inversion of Control)容器**:Spring 的核心特性是...
这个“Spring常用包”包含了Spring框架中最为常用的部分,适合初学者和开发者快速上手。 1. **Spring Core**: - **Bean Factory**:它是Spring的核心,负责管理对象的生命周期和依赖关系,通过XML或注解配置来...
Spring框架是Java开发中最常用的轻量级框架之一,它的出现极大地简化了企业级应用的开发。本篇文章将针对Spring框架的常用模块进行详细介绍,旨在帮助初学者理解和掌握Spring的核心概念。 1. **IoC(控制反转)与DI...
Spring框架是Java开发中最常用的轻量级框架之一,它的核心在于IoC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)。在本压缩包中,你可能会找到一系列与Spring相关的jar包,...
在"spring boot 常用数据库操作例子"这个示例中,我们可以看到几个关键点: 1. **多数据库实例**:Spring Boot支持配置多个数据源,这在需要连接不同数据库或者有分库分表需求的场景下非常有用。通过不同的配置文件...
Spring框架是Java开发中最常用的轻量级框架之一,它的核心特性包括依赖注入(Dependency Injection, DI)和面向切面编程(Aspect-Oriented Programming, AOP)。在进行Spring的学习时,常常需要一些必备的JAR包来...
Spring MVC提供了灵活的控制器接口、视图解析以及数据绑定等功能,极大地简化了Web应用的开发。 5. **Spring Test**:测试是软件开发的重要环节,Spring Test模块提供了对Spring应用的单元测试和集成测试的支持,...
2. **Spring Boot Starter Web**:这是 Spring Boot 的核心组件之一,提供了使用 Spring MVC 开发 Web 应用的功能,包括对 JSON 支持、静态资源处理等。 3. **RESTful API** 设计: - 使用 `@RestController` 注解...
这个“java spring jdbc 常用jar包集合.zip”文件很可能包含了实现Spring JDBC功能所需的各类库。在这个压缩包中,你可能会找到如下的核心组件: 1. **Spring Framework JAR**: - `spring-context.jar`:提供应用...
以下是对Spring常用包的详细解析: 1. **spring-core**:这是Spring框架的基础,包含了核心工具类和资源处理。其中,BeanUtils、ClassUtils等工具类提供了通用的Java对象操作,Resource接口则用于处理各种类型的...
1. **依赖注入(Dependency Injection, DI)**:Spring的核心特性之一,它允许组件之间的依赖关系在运行时被外部容器管理,而不是由组件自身负责。通过XML配置、注解或Java配置方式,可以轻松实现对象间的依赖关系。...
它集成了大量常用的第三方库配置,如 JDBC、MongoDB、JPA、RabbitMQ、Quartz 等,使得开发者可以快速地创建一个独立运行的、生产级别的基于 Spring 的应用。 在“spring boot 整合常用技术”中,我们主要关注以下几...
Spring框架是Java开发中最常用的轻量级开源框架之一,它为构建企业级应用程序提供了全面的解决方案。这个压缩包中包含的是一些Spring框架的核心jar包,对于理解和使用Spring框架至关重要。接下来,我们将深入探讨...
Spring会创建一个代理对象,该对象实现了与目标对象相同的接口,并在调用方法时插入切面逻辑。这种方式的优点是可以在不修改目标类的情况下添加新功能,但缺点是如果目标对象没有实现接口,就无法使用此方法。 二、...