原文: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. [bootstrap]模态框总结

    <!--data-backdrop禁止点击空白处关闭模态框,也可以js设置,其他参数可参考官网模态框文档--> <div class="modal fade" i ...

  2. zabbix3.0问题及解决方法

    一.问题:Zabbix agent on T2 is unreachable for 5 minutes         解决:1.进入zabbix service端 vim /etc/zabbix/ ...

  3. April 26 2017 Week 17 Wednesday

    We read the world wrong and say that it deceives us. 我们把世界看错了,反而说它欺骗了我们. It is not a cakewalk to see ...

  4. 会说话的ABAP report

    report z. INCLUDE ole2incl. DATA: ole   TYPE ole2_object,       voice TYPE ole2_object,       text   ...

  5. POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)

    题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...

  6. HDU(3560)成环,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3560 并查集查有几个块,修改了之前我的一个方法(用什么map),直接判断根节点的id是i的个数. 然后 ...

  7. 使用Excel管理命令输出

    效果图:(饼状图为后添加) 实现代码:

  8. frcnn_train_data_param的distort_param实现

    frcnn_train_data_param frcnn_train_data_param { source: "./data/train_list.txt" root_folde ...

  9. Can Microsoft’s exFAT file system bridge the gap between OSes?

    转自:http://arstechnica.com/information-technology/2013/06/review-is-microsofts-new-data-sharing-syste ...

  10. base_lr, blobs_lr

    caffe里面,原来以为是不可以随便调整学习率的,现在看来是可以的.base_lr是适用于所有层的学习率,而针对单个层,可以通过增加两个blobs_lr,用来调整该层的学习率,为什么是两个呢,因为一个 ...