在spring的配置文件中:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/> 

</beans>

我们会看到这样写:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="cn.test">
        <context:exclude-filter type="annotation"   expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能,即还隐式地在内部注册了
AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor。

因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

<context:component-scan/> 的base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

通过exclude-filter把所有@Controller注解的控制器组件排除。我们知道@Controller一般标注的是action,不用把action注入的什么地方。

转载于:https://blog.51cto.com/woshixy/949684

spring使用注解时配置文件的写法相关推荐

  1. 被各种注解搞晕了?那快来看看Spring Bean注解详解!

    前言 本篇博客中,我们将会讨论用于声明不同类型 Beans 的几种最常用的 Spring Bean 注解. 众所周知,Spring 容器中有许多配置 Bean 的方法,我们既可以通过 XML 配置,也 ...

  2. 【Spring】注解(AOP)不生效原因分析以及解决方案

    一 问题 我们在使用spring的注解时,稍不注意就很有可能会遇到注解没有生效的情况,比如下面这段代码,在发生类内调用时就出现了问题, import org.springframework.stere ...

  3. shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决

    shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决 from :http://blog.csdn.net/babys/article/ ...

  4. Spring原始注解和新注解(使用注解代替xml配置文件)

    Spring原始注解 spring的原始注解主要是为了替代xml文件中配置的 < bean > @Component:使用在类上用于实例化Bean @Controller:使用在web层类 ...

  5. Spring的properties属性配置文件和Spring常用注解

    Spring的properties属性配置文件 问题: 在学习了Spring的知识后,需要被Spring管理的资源都是通过配置文件的形式来告诉Spring容器对象管理谁以及如何管理.但是随着使用次数的 ...

  6. Spring Boot 注解配置文件自动映射到属性和实体类

    官网给出的配置文件大全: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#comm ...

  7. 【Spring 高级注解】@Configuration、@Bean、@ComponentScan

    Spring 高级注解(3.x以上) @Configuration:配置 Bean 基于注解开发使用日志 logback @Bean 注解 @Bean 注解的基本使用 @Bean 注解的注入与细节 @ ...

  8. Spring常用注解,以及注解的作用和简单用法

    这篇文章主要介绍了Spring当中的常见的一些注解,以及注解的简单用法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 @Configuration 作用:指定当 ...

  9. Spring系列之Spring常用注解总结

    参看博客:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺 ...

最新文章

  1. dabs是什么意思_单词flounder是什么中文意思
  2. 在FreeBSD上安装Bugzilla
  3. 各种NLP操作难实现?谷歌开源序列建模框架Lingvo
  4. python执行shell脚本、执行mongodb_Mongo shell 的基本操作和 Python shell 中使用 MongoDB...
  5. POJ 2106 Boolean Expressions (布尔表达式求值)
  6. java中检测数据波动_在pyspark数据帧中检测异常值
  7. “盛大文学”年终派对
  8. 【求助】小系统组成大系统所遇到的问题
  9. MsWord 操作总结
  10. 每天一个linux命令:【转载】less命令
  11. pytorch学习笔记(十八):Use-Gpu
  12. O2O营销模式(Online To Offline)
  13. 2015美亚杯团队赛
  14. 解决导出excel文件名中文乱码的问题
  15. 利用多组学整合鉴定人类疾病共享的和疾病特异性的宿主基因-微生物组关联
  16. ANSYS中Beam188\Beam189单元命令流提取最大应力
  17. python快速_Python3快速入门
  18. 公司旅游--金华武义二日游
  19. js一键批量打印_web页面的单页打印以及批量打印实现方法
  20. WAMP Server Localhost Shows 500 Internal Server Error

热门文章

  1. arm linux 64位,微软准备让Linux作为64位ARM Hyper-V客户机运行
  2. 设计模式复习-单例模式
  3. 设计模式-备忘录模式
  4. 【Android Gradle 插件】Extension 扩展类型 ( Module 引入插件类型 | application 插件 | library 插件 | Variants 变体列表 )
  5. 【Android 逆向】Android 进程注入工具开发 ( 系统调用 | Android NDK 中的系统调用示例 )
  6. 【SeeMusic】创建 SeeMusic 工程并编辑相关内容 ( 创建工程 | 导入 MIDI 文件 | 导入音频 | 导入视频 )
  7. 【Android 安装包优化】WebP 应用 ( 4.0 以下兼容 WebP | Android Studio 中使用 libwebp.so 库向下兼容版本 | libwebp 库测试可用性 )
  8. 【MATLAB】基本绘图 ( 绘制多图 | 设置图形对话框在 Windows 界面的位置和大小 | 在一个图形上绘制多个小图形 )
  9. 【计算理论】计算复杂性 ( 计算理论内容概览 | 计算问题的有效性 | 时间复杂性度量 | 输入表示 | 时间复杂度 )
  10. 【数据挖掘】关联规则挖掘 Apriori 算法 ( Apriori 算法过程 | Apriori 算法示例 )