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

Spring <context:annotation-config/> 解说(转),布布扣,bubuko.com

时间: 2024-10-12 19:58:59

Spring <context:annotation-config/> 解说(转)的相关文章

Spring 开启Annotation &lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;诠释及区别

Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的

Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException

org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'configController' for bean class [net.mingyang.modules.system.ConfigController] conflicts with existing, non-compatible bean definition of same

Spring Security Java Config Preview--官方

原文地址:[1]https://spring.io/blog/2013/07/02/spring-security-java-config-preview-introduction/ [2]https://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/ [3]https://spring.io/blog/2013/07/04/spring-security-java-config-previe

[org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on

<2015-4-10 下午02时55分35秒 CST> <Info> <Security> <BEA-090905> <Disabling the CryptoJ JCE Provider self-integrity check for better startup performance. To enable this check, specify -Dweblogic.security.allowCryptoJDefaultJCEVerifica

Spring MVC annotation configuration

1. Web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns

Context namespace element &#39;annotation-config&#39; and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser]

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class pa

Context namespace element &#39;component-scan&#39; and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is java.lang.IllegalStateException: Context

Spring中用java config简化xml配置

个人感觉还是这种方式配置方式最灵活了. package com.baobaotao.conf; public class LogDao {     public void print(){         System.out.println("helloworld");     } } package com.baobaotao.conf; public class UserDao {     public void print(){         System.out.print

Spring之Annotation的使用

package com.fxr.spring.action; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.fxr.spring.model.User; import com.fxr.spring.ser