1. 程式人生 > >Spring.xml中配置註解context:annotation-config和context:component-scan簡述

Spring.xml中配置註解context:annotation-config和context:component-scan簡述

ati RoCE persist font 註解 per 以及 factory ann

XML中context:annotation-config和context:component-scan簡述


<context:annotation-config/>                           中文意思:<上下文:註解——配置/>

<context:component-scan base-package=" 掃描路勁" annotation-config=true/>   中文意思:<上下文:部件——掃描 基礎——包=“掃描路徑”

註解——配置=true/>(annotation-config=true是默認的配置可省略不寫)

1.<context:annotation-config/> 

<context:annotation-config/>配置的作用:是向Spring容器註冊一次註入四個BeanPostProcessor

  • AutowiredAnnotationBeanPostProcessor
  • CommonAnnotationBeanPostProcessor
  • PersistenceAnnotationBeanPostProcessor
  • RequiredAnnotationBeanPostProcessor

AutowiredAnnotationBeanPostProcessor舊版註入方式<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>作用是能使用@Autowired註解

CommonAnnotationBeanPostProcessor舊版註入方式<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor

"/>作用是能使用@Resource、@PostConstruct、@PreDestroy註解

PersistenceAnnotationBeanPostProcessor舊版註入方式<bean class="org.springframework.beans.factory.annotation.PersistenceAnnotationBeanPostProcessor"/>作用是能使用@PersistenceContext註解;

RequiredAnnotationBeanPostProcessor舊版註入方式<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>作用是能使用@Required註解;

<context:annotation-config/>的總結即在XML文件中配置<context:annotation-config/>不需要再配置上面四個ben對象;

2.<context:component-scan base-package=" 掃描路勁" annotation-config=true/>


  作用:具有<context:component-config/>註解的功能,還能對指定的package下掃描以及註冊javabean

Spring.xml中配置註解context:annotation-config和context:component-scan簡述