`
struggle2036
  • 浏览: 6511 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring之 <context:component-scan>使用说明

 
阅读更多

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

注意:如果配置了<context:component-scan>那 么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。另 外<context:annotation-config/>还提供了两个子标签

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指定的包中有的子包是不含有注解了,所以不用扫描,此时可以指定<context:exclude-filter>来进行过滤,说明此包不需要被扫描。综合以上说明

Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描

分享到:
评论

相关推荐

    Spring扫描器—spring组件扫描使用详解

    在Spring框架中,`&lt;context:component-scan/&gt;`元素是核心组件扫描的基石,它允许我们自动检测和注册...在实际项目中,结合使用`@Component`家族注解和`&lt;context:component-scan/&gt;`,能够构建出高效、灵活的Spring应用。

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    然而,在配置过程中,如果遇到“元素 'context:component-scan' 的前缀 'context' 未绑定”的错误,这意味着Spring无法识别和解析这个元素,因为缺少了对应的命名空间定义。 这个问题的根源在于XML配置文件中没有...

    spring组件扫描contextcomponent-scan使用详解.pdf

    Spring 组件扫描&lt;context:component-scan/&gt;使用详解 在 Spring 框架中,组件扫描是指通过注解和 XML 配置来自动检测和加载Bean的过程。下面将详细介绍&lt;context:component-scan/&gt;标签的使用方式和原理。 一、...

    spring4-hibernate4-struts2整合

    &lt;context:component-scan base-package="me.gacl.dao,me.gacl.service"/&gt; &lt;/beans&gt; ``` 4. **创建配置属性文件**(`config.properties`): - 这个文件通常用于存储数据库连接信息、其他配置参数等。 - 示例...

    Spring注解详解

    &lt;/context:component-scan&gt; ``` 使用注解过滤某些类: ```xml &lt;context:component-scan base-package="com.example"&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype....

    spring-mvc-4.2.xsd.zip

    这个文件包含了所有在Spring MVC 4.2版本中可以使用的XML配置元素和属性,例如`&lt;mvc:annotation-driven&gt;`、`&lt;bean&gt;`、`&lt;context:component-scan&gt;`等。这些配置元素允许开发者声明式地配置控制器、视图解析器、转换...

    集成springmvc spring hibernate的配置

    &lt;/context:component-scan&gt; ``` 2. **数据库连接配置**:我们需要从`jdbc.properties`文件中读取数据库连接信息。使用`context:property-placeholder`标签将属性文件加载到Spring上下文中。然后,配置数据源`...

    spring-framework-4.3.4.RELEASE-schema

    同时,`&lt;context:component-scan&gt;`元素则能自动扫描并注册带有特定注解的bean,进一步降低了配置复杂性。 在Spring Framework 4.3.4.RELEASE中,XML Schema的使用极大地提高了配置的可读性和可维护性,使得开发者...

    spring3零配置注解实现Bean定义(包括JSR-250、JSR-330)

    通过使用`&lt;context:component-scan&gt;`标签配合特定的注解(如`@Component`、`@Service`等),可以完全避免XML配置的使用,从而让开发者能够更加专注于业务逻辑的实现。同时,Spring对JSR-250和JSR-330等标准的支持也...

    15、spring 配置以及使用 1

    在这个主题中,我们将深入探讨`&lt;context:annotation-config&gt;`与`&lt;context:component-scan&gt;`的区别,事务管理器的配置,以及Spring开发环境的选择和数据源的配置。 1. `&lt;context:annotation-config&gt;`和`&lt;context:...

    spring-framework-4.2.4.RELEASE-schema

    在这个版本中,Spring对测试模块也做了加强,`&lt;context:component-scan&gt;`可以在测试环境中自动扫描和注册组件,`&lt;test&gt;`标签则用于定义单元测试的配置。 总结来说,Spring Framework 4.2.4.RELEASE的Schema特性...

    Spring3.X @MVC - (一)重要的配置文件

    1. **`&lt;context:component-scan&gt;`**: 这个元素告诉Spring扫描指定的包,寻找带有特定注解(如@Controller、@Service、@Repository和@Component)的类,以便自动注册这些bean到Spring容器中。这是实现DI的关键步骤。 ...

    spring mvc

    2. `&lt;context:component-scan&gt;`: - 这个元素告诉Spring扫描指定包及其子包,寻找带有`@Controller`注解的类,并将它们作为bean进行管理。 3. `&lt;mvc:annotation-driven&gt;`(通常也会包含,但此处未提供): - 这个...

    spring 的Annotation方式

    本文旨在深入探讨Spring框架中基于注解的依赖注入机制,特别是`@Repository`、`@Service`、`@Controller`和`@Component`等核心注解的使用方法,以及如何利用`&lt;context:component-scan&gt;`自动扫描功能,实现类级别的...

    使用Spring2.5的Autowired实现注释型的IOC

    在上面的配置文件中,我们使用了 `&lt;context:annotation-config/&gt;` 和 `&lt;context:component-scan base-package="testspring.main"/&gt;` 两个标签。 `&lt;context:annotation-config/&gt;` 用于启用注释型的 IOC,而 `&lt;context...

    spring-framework-3.2.0.RC2-schema.zip

    `context`命名空间下的 `&lt;context:component-scan&gt;`、`&lt;context:property-placeholder&gt;`等元素,用于扫描组件、加载外部属性文件,增强了Spring的应用范围和灵活性。 "cache"模块则提供了缓存抽象,支持如 EhCache...

    关于Spring的spring-beans-xsd和tx-xsd aop-xsd等

    它包括了如`&lt;context:component-scan&gt;`、`&lt;context:annotation-config&gt;`等元素,使得我们可以方便地启用注解驱动的配置和组件扫描,发现并自动装配带有特定注解的类。此外,它还支持消息源、AOP代理、事件监听等特性...

    Spring环境配置

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; ``` 1. **DispatcherServlet**:这是Spring MVC框架的核心组件,负责处理HTTP请求并分发到具体的控制器。`...

    spring-mvc 注解方式xml配置

    `&lt;context:component-scan&gt;`注解扫描指定的包,查找带有注解的组件,包括我们的`@Controller`。`&lt;mvc:annotation-driven&gt;`则启用了Spring MVC的注解支持。 最后,`web.xml`是应用的部署描述符,配置`...

Global site tag (gtag.js) - Google Analytics