首页 > 代码库 > Spring文档苦读【5】【<context:annotation-config/>】

Spring文档苦读【5】【<context:annotation-config/>】

如何配置Spring注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

描述

上述配置,会隐式的配置一下PostProcessor,具体:AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor, 以及 RequiredAnnotationBeanPostProcessor。

并,如何你在DispatcherServlet对应的WebApplicationContext配置<context:annotation-config/>,其只会@Autowired,而不会检查Service下的注解。

AutowiredAnnotationBeanPostProcessor

声明@Autowired注解

CommonAnnotationBeanPostProcessor

声明@Resource、@PostConstruct、@PreDestroy等注解

PersistenceAnnotationBeanPostProcessor

声明@PersistenceContext注解

RequiredAnnotationBeanPostProcessor

声明@Required注解

关注

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

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

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

摘录:http://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html

Spring文档苦读【5】【<context:annotation-config/>】