zoukankan      html  css  js  c++  java
  • 关于<context:annotation-config/>配置

    对于spring项目的一些配置,一直感到有些混乱,今天看到一前辈总结的特别好,把自己的理解贴在这里,有不当的地方,后续继续学习;

    当我们使用@Autowired、@Required等这些注解时,就要在spring容器里声明这些注解对应的java类的bean,那么需要在xml中进行配置;

    如声明@Autowired:

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

    声明@Required:

    <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:component-scan base-package="包全路径名称"/>

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

    当然这里要注意的是配置<context:component-scan/>要加上相应的约束

    xmlns:context="http://www.springframework.org/schema/context"
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    

      

  • 相关阅读:
    C# 常用小点
    WebClient下载文件
    redis常见错误处理
    win2008R2环境配置
    C#程序员知识体系
    js中json的添加和指定位置的删除
    关于本博客的美化
    箭头函数中可改变this作用域,回调函数用箭头函数this指向page,自定义事件用箭头函数this指向undefined
    json_decode 和 json_encode 区别
    tp6中使用微信支付sdk
  • 原文地址:https://www.cnblogs.com/yangh965/p/5102077.html
Copyright © 2011-2022 走看看