• 关于Spring中的<context:annotation-config/>配置


      一些小疑问,注解装配在默认情况下是不开启的,为了使用注解装配,我们必须在Spring配置文件中配置 <context:annotation-config/>元素。但是在看项目源码的时候没有这一配置,就有些疑惑,找到了https://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html的博文,也就明白了

      当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
      使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

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

      使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

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

      类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
      这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:

    <context:annotation-config/> 

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

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

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

  • 相关阅读:
    parseInt()的用法
    报文
    express的中间件与next()
    前后端分离与前后端不分离
    jQuery中四个绑定事件的区别 on,bind,live,delegate
    TCP传输的三次握手四次挥手策略
    报文
    HTTP和HTTPS以及两者的区别
    前后端不分离与分离
    express中间件的next()方法
  • 原文地址:https://www.cnblogs.com/winterfells/p/9402435.html
Copyright © 2020-2023  润新知