Spring中的<context:annotation-config/>配置

  当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:

  使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

  使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

  类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。

  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:

<context:annotation-config/> 

  另,在我们使用注解时一般都会配置扫描包路径选项:

<context:component-scan base-package="pack.pack"/>

  该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。

备注:

在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
    ...
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ...
        <context:annotation-config/>
    </beans>
时间: 2024-08-06 01:45:42

Spring中的<context:annotation-config/>配置的相关文章

解决Spring中使用context:component-scan命名空间配置错误

** nested exception is org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 74; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明. 原因:Spring命名空间配置错误,缺少相应的spring-bean.解决:<?xml version="1.0" encoding="UTF-8&qu

Spring中IOC的Annotation实现

Spring中IOC的Annotation实现: 此处特别注意每个类中注释的注解及相关文字说明,可以更好的理解Spring中IOC的Annotation实现! 首先beans.xml中需要引入 context 的schema: [html] view plaincopyprint? <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework

spring中classpath和classpath*的配置区别

转自:http://www.micmiu.com/j2ee/spring/spring-classpath-start/ —————————————————————————————————————————— 在使用spring时,经常会看到类似 classpth:.classpath*: 这样的前缀,不管是加载spring xml配置文件还是其配置文件中加载资源文件都会看到这两种前缀配置,其实这两种前缀是有区别的,下面将举例详细解释. [一].测试项目准备 我们以spring中加载propert

使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法

Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法 获取Class类实例的方法: 类名.class 实例名.getClass() Class.forName(className) public class RefTest { @Test public void testRef(){ //Class cls = RefTest.class; //Class.forName("com.jboa.service.RefTest"); //new RefTest()

spring中quatz的多定时任务配置图文详解

近来公司让用quatz框架做定时功能,而且还是执行多定时任务,真是苦恼. 虽然从网上搜了很多资料,但是写法上不太尽如人意,最后还是请教了螃蟹大神,给的配置建议就是简单啊,现在拿来分享下: 这里我们需要的有两部分,一个是java中的处理类,一个是quatz的配置文件,截图如下applicationContext_quartz.xmlquatz的配置文件地址地址:http://www.itxxz.com/a/kuangjia/kuangjiashili/2014/0602/10.html java中

Spring 框架的概述以及Spring中基于XML的IOC配置

Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器.框架.一站式 优势: 方便解耦:做到编译期不依赖,运行期才依赖 AOP的支持 声明式事务的支持 方便程序的测试 方便整合各种框架 降低JavaEE API的使用难度 Spring源码很厉害 解耦: 耦合包括:类之间的和方法之间的 解决的思路: 在创建对象的时候用反射来创建,而不是new 读取配置文件

关于Spring中的&lt;context:annotation-config/&gt;配置

当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean: <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> 使用 @Req

关于Spring中的&lt;context:annotation-config/&gt;配置(开启注解)

当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean: <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> 使用 @Req

Spring中基于Java的容器配置(二)

使用@Configuration注解 @Configuration注解是一个类级别的注解,表明该对象是用来指定Bean的定义的.@Configuration注解的类通过@Bean注解的方法来声明Bean.通过调用注解了@Bean方法的返回的Bean可以用来构建Bean之间的相互依赖关系,可以通过前文来了解其基本概念. 注入inter-bean依赖 当@Bean方法依赖于其他的Bean的时候,可以通过在另一个方法中调用即可. @Configuration public class AppConfi