`
lin_zy
  • 浏览: 2266 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Spring <context:annotation-config/>

 
阅读更多
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。

注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下

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

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

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

一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。

   不过,呵呵,我们使用注解一般都会配置扫描包路径选项

<context:component-scan base-package=”XX.XX”/>

    该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
分享到:
评论

相关推荐

    异常解决:错误:namespace element 'annotation-config' … on JDK 1.5 and higher

    2. **XML配置问题**:确保你的Spring配置文件(如`applicationContext.xml`)正确包含了`&lt;context:component-scan&gt;`或`&lt;context:annotation-config&gt;`元素,它们是启用注解配置的关键。 3. **编译器设置**:检查你的...

    spring_MVC源码

    10. &lt;context:annotation-config /&gt; 11. &lt;!-- 把标记了@Controller注解的类转换为bean --&gt; 12. &lt;context:component-scan base-package="com.mvc.controller" /&gt; 13. &lt;!-- 启动Spring MVC的注解功能,...

    spring-mybatis-spring-2.0.4.zip

    &lt;property name="configLocation" value="classpath:mybatis-config.xml"/&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;property name...

    Spring基于注释(Annotation)的配置.pdf

    ### Spring基于注释...`&lt;context:annotation-config/&gt;`是Spring提供的一个配置选项,它可以自动注册多个BeanPostProcessor,包括`AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`...

    Spring的Annotation配置相关讲义

    `&lt;context:annotation-config&gt;`元素会扫描容器中的所有Bean,查找并处理如`@Autowired`、`@Required`、`@PostConstruct`等注解,实现依赖注入。 `&lt;context:component-scan&gt;`元素用于指定需要扫描的包,这样Spring会...

    关于Spring的spring-beans-xsd和tx-xsd aop-xsd等

    它包括了如`&lt;context:component-scan&gt;`、`&lt;context:annotation-config&gt;`等元素,使得我们可以方便地启用注解驱动的配置和组件扫描,发现并自动装配带有特定注解的类。此外,它还支持消息源、AOP代理、事件监听等特性...

    spring使用annotation整合dwr笔记

    - **配置DWR Annotation Config**:利用`&lt;dwr:annotation-config/&gt;`标签启用注解支持,使得开发者能够使用`@RemoteProxy`和`@RemoteMethod`等注解来标注需要暴露给客户端的方法。 ```xml &lt;!-- 配置DWR注解支持 --&gt;...

    Spring配置文件spring-context.zip

    8. `&lt;context:annotation-config&gt;`:激活对注解的处理,如@Autowired、@Required等。 9. `&lt;bean factory-method=""&gt;`:通过工厂方法创建bean,而非使用默认构造器。 10. `&lt;aop:config&gt;`和`&lt;aop:aspect&gt;`:用于配置...

    spring mvc基础

    &lt;param-value&gt;classpath:config/springAnnotation-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- 启动Spring上下文监听器 --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context....

    springmvc+spring+mybatis

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath*:...

    [新手-图文]整合ssm框架-从mybatis到spring-mybatis再到ssm-sping-mybatis-spingmvc

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web....

    Spring注解详解

    &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/&gt; ...

    自己写网页spring框架搭建

    - 使用`&lt;context:annotation-config/&gt;`来启用Spring MVC的注解支持。 - **控制器扫描**: - 通过`&lt;context:component-scan base-package="controller"/&gt;`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图...

    spring+ibatis集成文档

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;!-- 扫描Dao接口所在的包 --&gt; &lt;context:component-scan base-package="ch10.SpringAndIbatis"/&gt; &lt;/beans&gt; ``` 这里主要包括数据源配置、...

    集成springmvc spring hibernate的配置

    &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.mvc.*"&gt; &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component...

    有关Spring的注解

    &lt;context:annotation-config/&gt; &lt;/beans&gt; ``` 这段配置会隐式地注册多个注解处理器,如`AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`等,它们负责处理相应的注解。 - **`@...

    Springboot项目整合JSP页面

    &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;!-- 以下是其他可能的 Spring Boot 依赖,如 spring-boot-starter-data-jpa 等 --&gt; ... &lt;/dependencies&gt; ``` ...

    Spring AOP配置源码

    &lt;/beans&gt;&lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.spring.*"/&gt; 两行为开启spring的注解配置 &lt;aop:aspect id="aspect" ref="logIntercepter"&gt; 引入具体的AOP操作类 &lt;aop:pointcut ...

    spring-framework-4.3.4.RELEASE-schema

    通过`&lt;aop:config&gt;`和`&lt;aop:advisor&gt;`等元素,我们可以定义切面、通知和匹配规则,实现代码的解耦和模块化。在这一版本中,Spring对AOP的Schema进行了细化,使得切面的定义更加精确和灵活。 再者,Spring的数据库...

Global site tag (gtag.js) - Google Analytics