spring里面的context:component-scan

原文: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>
时间: 2024-08-08 09:40:26

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

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

/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://

Spring mvc解读&lt;context:component-scan/&gt;

<context:component-scan/>标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件.<mvc:annotation-driven/>是告知Spring,我们启用注解驱动.然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求. 一.<context:component-scan/> 想必@Component,@Reposit

Spring MVC 解读——&lt;context:component-scan/&gt;

转自:http://my.oschina.net/HeliosFly/blog/203149 作者:GoodLoser. Spring MVC 解读---<context:component-scan/> 注解是骑士魂牵梦绕的美丽公主,也是骑士的无法摆脱的噩梦... 一.<context:component-scan/> 想必@Component,@Repository,@Service,@Controller几个常用的Type-Level的Spring MVC注解,大家都很清楚他

Spring 开启Annotation &lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;诠释及区别

Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的

spring注解注入:&lt;context:component-scan&gt;详解(转)

spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 我们首先看一个注解注入的实际例子,然后再详细介绍context:component-scan的使用. 如果你已经在用spring mvc的注解配置,那么你一定已经在使用注解注入了,本文不会涉及到spring mvc,我们用一个简单的例子来说明问题. 本例中我们会定义如下类: PersonService类,给上层提供

spring注解注入:&lt;context:component-scan&gt;详解

spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 我们首先看一个注解注入的实际例子,然后再详细介绍context:component-scan的使用. 如果你已经在用spring mvc的注解配置,那么你一定已经在使用注解注入了,本文不会涉及到spring mvc,我们用一个简单的例子来说明问题. 本例中我们会定义如下类: PersonService类,给上层提供

如何获取listview里面的edittext或者RadioGroup的值,涉及到引发的混乱现象

最近要实现从数据库读数据,该数据对应listview的item布局里面的RadioButton值,并且item布局里面还有EditText的控件. 如何将每一条对应的listview对应值获取出来呢? 实现原理是在作为ArrayAdapter的参数的数据源对象(如数组,集合),举例: ArrayList<MyClass> arrayList = new ArrayList<MyClass>(); ArrayAdapter arrayAdapter = new ArrayAdapte

自定义Button,复写里面的onKeyDown,不起作用

李刚的Android疯狂讲义真是"疯狂",浪费了3天时间,到底是他的代码有问题,还是怎么的不得而知. 问题描述:他的书里面第3.3基于回调事件处理Propagation的例程.是为了掩饰基于回调事件传播的例程序,源代码如下: MyButton.java public class MyButton extends Button { public MyButton(Context context , AttributeSet set) { super(context , set); } @

koa2 中间件里面的next到底是什么

koa2短小精悍,女人不爱男人爱. 之前一只有用koa写一点小程序,自认为还吼吼哈,知道有一天某人问我,你说一下 koa或者express中间件的实现原理.然后我就支支吾吾,好久吃饭都不香. 那么了解next的最好办法是什么, 百度,谷歌,知乎?  没错,肯定有用,我觉得最有用的是看源码和debug去理解. 先看下面的一段代码 ,会输出什么,只会输出  X-Response-Time const Koa = require('koa'); const app = new Koa(); // x-