`

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/>
移除了。


引自:http://mushiqianmeng.blog.51cto.com/3970029/723880
分享到:
评论

相关推荐

    15、spring 配置以及使用 1

    在这个主题中,我们将深入探讨`&lt;context:annotation-config&gt;`与`&lt;context:component-scan&gt;`的区别,事务管理器的配置,以及Spring开发环境的选择和数据源的配置。 1. `&lt;context:annotation-config&gt;`和`&lt;context:...

    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包扫描配置的项目

    在Spring中,包扫描是通过`&lt;context:component-scan&gt;`标签实现的,该标签位于XML配置文件中。这个标签告诉Spring去指定的包及其子包下查找标记为`@Component`、`@Service`、`@Repository`和`@Controller`的类,这些...

    SpringMVC知识点.doc

    &lt;param-value&gt;/WEB-INF/spring-mvc-config.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; ... &lt;servlet-mapping&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;url-...

    spring注解

    &lt;context:annotation-config /&gt; ``` 这将隐式地向 Spring 容器注册 `AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`PersistenceAnnotationBeanPostProcessor` 和 `...

    SSM三大框架的整合.docx

    &lt;bean id="logbackConfigLocation" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt; &lt;property name="targetObject" value="java.lang.System" /&gt; &lt;property name="targetMethod" ...

    java 小型环境搭建 SpringMVC

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;5.3.20&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet-api...

    Springboot项目整合JSP页面

    【Spring Boot 整合 JSP 页面】在传统的 Spring MVC 项目中,配置访问 JSP 页面相对简单,然而,Spring Boot 采用内置的 Servlet 容器(如Tomcat),默认并不支持 JSP,主要是因为 Spring Boot 强调快速开发,简化...

    使用Eclipse构建Maven的SpringMVC项目

    6. **创建Spring配置文件**:在WEB-INF下创建spring-mvc-config.xml,配置Spring MVC的bean。例如: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00

    -- 配置项 --&gt; &lt;/plug-in&gt; &lt;!-- 配置Action --&gt; &lt;action path="/yourAction" type="com.yourpackage.YourAction" input="/login.jsp" name="yourFormName" scope="request" validate="true"&gt; &lt;forward name=...

    spring与mybatis整合配置文档

    这里通过`&lt;context:property-placeholder&gt;`加载了`dbconfig.properties`中的配置项,确保了数据库连接参数的外部化管理。 ```xml &lt;context:property-placeholder location="classpath:dbconfig.properties"/&gt; ...

    oracle连接池配置[归类].pdf

    &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.bjsxt"/&gt; ``` 以上就是Oracle连接池在S2SH框架中的配置过程,涵盖了项目环境、依赖包、Spring配置文件中的SessionFactory配置、数据源、...

    Spring下的子框架SpringMVC实现单个文件上传功能

    &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;/WEB-INF/config/...

    spring icefaces hibernate 整合

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; ``` 2. **Spring MVC Dispatcher Servlet**:如果使用Spring MVC,还需要配置`DispatcherServlet`来处理...

    jbpm 4.3 与 spring 集成 .doc

    &lt;context:annotation-config /&gt; &lt;bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;hibernate....

    Mybatis +Spring 整合

    &lt;artifactId&gt;spring-context&lt;/artifactId&gt; &lt;version&gt;5.x.x.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt; &lt;version&gt;5.x.x....

    Spring2.5详细注解.doc

    使用 `&lt;context:annotation-config /&gt;` 可以方便地、一次性的声明这四个 BeanPostProcessor。 传统上,我们使用 XML 文件来配置 Bean 和 Bean 之间的依赖关系。但是,使用基于注释的配置可以简化配置过程,提高开发...

Global site tag (gtag.js) - Google Analytics