极简设计

component-scan

component-scan

使用以下BeanPostProcessor时,配置中定义这些Bean显得比较笨拙,例如:

@Autowired注解

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

@Required

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

@Resource、@PostConstruct、@PreDestroy

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

@PersistenceContext

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

太不优雅

更优雅注册这些BeanPostProcessor的方式

<context:annotation- config/>

隐式地向 Spring容器注册4个BeanPostProcessor

  • AutowiredAnnotationBeanPostProcessor
  • RequiredAnnotationBeanPostProcessor
  • CommonAnnotationBeanPostProcessor
  • PersistenceAnnotationBeanPostProcessor

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

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

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

in java with : spring, java