<context:component-scan>的使用说明

在Spring框架中有这样一个标签元素:

<context:component-scan base-package="com.qunar.flight.interb2b.dubbodemo.task"/>

这个标签的意思就是:

在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件, 如果扫描到有@Component @[email protected]等这些注解的类,则把这些类注册为bean。

在这里我的包名是com.qunar.flight.interb2b.dubbodemo.task 可以知道在这个包名下一定有一些类加入了Component 注解,如下:

import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import qunar.tc.qschedule.config.QSchedule;
import qunar.tc.schedule.TaskHolder;
import qunar.tc.schedule.TaskMonitor;

@Component
public class QScheduleDemoTask {

    private static final int BATCH = 100;

    @QSchedule("f.inter_b2b.dubbodemo.demotask")
    public void processOrders() {

    }
}

注意:

如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。

另外还提供了两个子标签

1.<context:include-filter>

2.<context:exclude-filter>

在说明这两个子标签前,先说一下<context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等。所以如果仅仅是在配置文件中这么写

<context:component-scan base-package="tv.huan.weisp.web"/>

Use-default-filter此时为true那么会对base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。

可以发现这种扫描的粒度有点太大,如果你只想扫描指定包下面的Controller,该怎么办?此时子标签<context:incluce-filter>就起到了勇武之地。如下所示

<context:component-scan base-package="tv.huan.weisp.web .controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> 

这样就会只扫描base-package指定下的有@Controller下的java类,并注册成bean

但是因为use-dafault-filter在上面并没有指定,默认就为true,所以当把上面的配置改成如下所示的时候,就会产生与你期望相悖的结果(注意base-package包值得变化)

<context:component-scan base-package="tv.huan.weisp.web ">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>  

此时,spring不仅扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类

此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。这样就可以避免在base-packeage配置多个包名这种不是很优雅的方法来解决这个问题了。

另外在我参与的项目中可以发现在base-package指定的包中有的子包是不含有注解了,所以不用扫描,此时可以指定来进行过滤,说明此包不需要被扫描。综合以上说明

Use-dafault-filters=”false”的情况下:指定的不扫描,指定的扫描

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 16:39:33

<context:component-scan>的使用说明的相关文章

Spring MVC注解配置结合Hibernate的入门教程及其代码实例

原文:Spring MVC注解配置结合Hibernate的入门教程及其代码实例 源代码下载地址:http://www.zuidaima.com/share/1787210045197312.htm 1.概述 本文旨在搭建Spring MVC+Hibernate开发框架,通过一个简单的demo讲解Spring MVC的相关配置文件,以及通过注解方式实现简单功能. 开发框架:Spring+Spring MVC+Hibernate(Spring所用的版本为3.0.5). 数据库:MySQL(数据库名称

spring mvc 编写处理带参数的Controller

在上一随笔记录的基础上,现记录编写处理带有参数的Controller. @Controller //这个注解会告知<context:component:scan> 将HomeController自动检测为一个Bean@RequestMapping("/home")  //这是根Urlpublic class HomeController {        private UserService userService;        @Autowired    public

如何用Spring将Service注入到Servlet中

解决方法有两种(推荐使用第二种) 方法一: 直接重写Servlet的Init()方法,代码如下: public void init(ServletConfig servletConfig) throws ServletException { ServletContext servletContext = servletConfig.getServletContext(); WebApplicationContext webApplicationContext = WebApplicationCo

注解Annotation的IoC:从@Autowired到@Component

注解Annotation的IoC:从@Autowired到@Component 2017-01-20 目录 1 什么是注解2 不使用注解示例  2.1 com.springioc.animal.Monkey  2.2 com.springioc.animal.Tiger  2.3 com.springioc.bean.Zoo  2.4 com.springioc.main.AppMain  2.5 Beans.xml3 使用注解@Autowired示例  3.1对成员变量使用@Autowired

context:component-scan

Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected. Note: This tag implies the effects of the 'ann

applicationContext.xml和dispatcher-servlet.xml的区别

在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢? Spring 官方文档介绍如下: Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml defines the beans for the "root webapp context

SpingMVC 核心技术帮助文档

SpringMVC 框架使用技巧 声明:本篇文档主要是用于参考帮助文档,没有实例,但几乎包含了SpringMVC 4.2版本的所有核心技术 对于觉得篇幅长的文档,建议大家使用快捷键crtl + F,搜索关键字查询较为方便. 欢迎加群JAVA编程交流群 574337670 21.1 Spring Web MVC框架简介 Spring的模型-视图-控制器(MVC)框架是围绕一个 DispatcherServlet 来设计的,这个Servlet会把请求分发给各个处理器,并支持可配置的处理器映射.视图渲

Spring Batch示例: 读取CSV文件并写入MySQL数据库

Spring Batch示例: 读取CSV文件并写入MySQL数据库 GitHub版本: https://github.com/kimmking/SpringBatchReferenceCN/blob/master/01_introduction/Spring_Batch_MySQL.md 原文链接: Reading and writing CVS files with Spring Batch and MySQL 原文作者: Steven Haines - 技术架构师 下载本教程的源代码: S

javax inect

Spring 3 and JSR-330 @Inject and @Named example By mkyong | September 16, 2012 | Viewed : 86,399 times Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard @Inject i

DEPENDENCY INJECTION EXAMPLE USING SPRING

Dependency Injection The Spring Framework is literally built around the concept of Dependency Injection. In this post, we’ll take a look at a simple example of Dependency Injection using the Spring Framework. If you want a deeper dive on Dependency I