原文:http://jinnianshilongnian.iteye.com/blog/1762632

component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean

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

今天在看一个代码项目时,看到有人使用了类似如下配置。看字面意思是要把@Service的注解包括进来,把@Controller的注解排除在外。

然后那个代码分别在Springmvc的配置文件里面把@Service和@Repository排除,在Spring配置文件中把@Controller排除。

至于为什么要排除,我起初以为性能问题,比如springmvc只需要关注controller,spring只需要关注service和repository。但是这是错误的认识,具体原因在最前面那个链接里有。

同时include-filter也可以把base-packge包之外的其他包的给包括进来。

include-filter如果放在exclude-filter后面则会报错。

<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

同时,context:component-scan还有一个默认属性use-default-filters="true",默认是true,即扫描Component(包括service、controller和repository)

当只想包括某个注解时,需要显式关闭这个属性

<context:component-scan base-package="com.target" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

spring里面的context:component-scan的更多相关文章

  1. spring里面的ioc的理解?

    spring里面的ioc就是控制反转,其实现核心是DI(依赖注入),控制反转不向以前java代码里面,通过new关键字来实现创建对象,这样每段代码之间的耦合度就比较高,为了降低每个小模块之间的耦合度, ...

  2. jquery对象里面的context参数

    jquery源码: jQuery = function( selector, context ) { // The jQuery object is actually just the init co ...

  3. spring 里面的StringUtils,先放这儿,有时间研究吧

    /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Vers ...

  4. redis如何在spring里面的bean配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  5. [Spring Boot] Use Component Scan to scan for Bean

    Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...

  6. spring源码分析-core.io包里面的类

    前些日子看<深入理解javaweb开发>时,看到第一章java的io流,发觉自己对io流真的不是很熟悉.然后看了下JDK1.7中io包的一点点代码,又看了org.springframewo ...

  7. Spring MVC 解读——<context:component-scan/>

    转自:http://my.oschina.net/HeliosFly/blog/203149 作者:GoodLoser. Spring MVC 解读---<context:component-s ...

  8. Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别

    Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...

  9. spring配置注解context:annotation-config和context:component-scan区别

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...

随机推荐

  1. 利用jQuery做登录界面的验证码

    主要是为了有效防止机器恶意注册,对某一个特定已注册用户用特定程序暴力破解方式进行不断的登陆尝试.验证码是现在很多网站注册/登录时必填的, 虽然对用户可能有点麻烦,但是对网站/社区来说这个功能还是很有必 ...

  2. windows服务器间文件同步搭建步骤搜集

    Rsync https://www.cnblogs.com/janas/p/3321087.html https://yq.aliyun.com/ziliao/110867 subersion协议 h ...

  3. 使用函数BAPISDORDER_GETDETAILEDLIST读取S/4HANA中Sales Order行项目数据

    事务码MM03查看物料主数据,如下图所示的行项目数据,包含物料ID,描述信息,数量,单价等等: 使用如下代码进行行项目读取: DATA: ls_read TYPE order_view, lt_ite ...

  4. POJ-2151 Check the difficulty of problems---概率DP好题

    题目链接: https://vjudge.net/problem/POJ-2151 题目大意: ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 问 每队至少解出一题且冠军队至少解出N ...

  5. 数长方形有多少个?POJ(1693)

    题目链接:http://poj.org/problem?id=1693 解题报告: 随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形. #include ...

  6. UIView设置阴影无效的原因之一

    本想在底部的按钮设置个阴影, 代码如下: self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOffset ...

  7. python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型

    我们紧接上次,这里将建立数据库,创建第一个模型提示:这里我们不需要去一直启动,django会在我们ctrl+s的时候自动刷新并启动服务,很方便吧  1.数据库配置 现在,打开 vote_mysite/ ...

  8. Python简单线程间通信

    本节主要举一个简单的线程间通信的例子,利用线程安全的数据结构queue.Queue保存线程间通信的内容, import queue from threading import Thread from ...

  9. github上更新fork项目

    转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...

  10. JS - 给数组的原型添加去掉重复元素的distinct方法

    /* 调用完该方法,原数组只留下非重复的数据 返回一个数组,里面是依次出现的重复元素 */Array.prototype.distinct = function () {    var removeA ...