spring component-scan filter

这个文件中beans根节点下只有一个context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Repository,Controller注解修饰的类,而此处我们处于示例的目的,故意将use-default-filters属性设置成了false。

context:component-scan节点允许有两个子节点<context:include-filter>和<context:exclude-filter>。filter标签的type和表达式说明如下:

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ語法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter

在我们的示例中,将filter的type设置成了正则表达式,regex,注意在正则里面.表示所有字符,而\.才表示真正的.字符。我们的正则表示以Dao或者Service结束的类。

我们也可以使用annotaion来限定,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<context:component-scan base-package="cn.outofmemory.spring" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> 
	 </context:component-scan>
</beans>

这里我们指定的include-filter的type是annotation,expression则是注解类的全名。

另外context:conponent-scan节点还有<context:exclude-filter>可以用来指定要排除的类,其用法和include-filter一致。

<转:http://outofmemory.cn/java/spring/spring-DI-with-annotation-context-component-scan>

时间: 2024-07-30 10:17:03

spring component-scan filter的相关文章

注解方式定义的spring component打jar后,扫描失败的可能原因

情况是这样的:web工程采用了ssh框架,dao和service都是通过annotation方式注入的,工程运行正常.后来把service和dao打成jar放在工程的lib目录下,问题来了,配置没改动,结果就是不能自动注入dao和service.但是如果把dao和service在spring配置文件中通过xml文件配置,这些component能找到. 搜索了一把,发现这个问题比较常见,大部分帖子说的是在打jar包的时候add entity directory,仔细看了下,我的jar包是通过mav

Spring框架之Filter应用

在web.xml中进行配置,对所有的URL请求进行过滤,就像"击鼓传花"一样,链式处理. 配置分为两种A和B. A:普通配置 在web.xml中增加如下内容: <filter> <filter-name>permissionFilter</filter-name> <filter-class>com.taobao.riskm.filter.PermissionFilter</filter-class> </filter&

Not registered via @EnableConfigurationProperties or marked as Spring component

SpringBoot中,将类中的属性和配置文件中的配置进行绑定时出现以下的问题: 当使用@ConfigurationProperties时IDEA顶部出现这样的提示: 按照提示点击跳转到官方文档,接着在pom.xml中添加如下的配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artif

Spring Cloud Zuul Filter 和熔断

转一篇很不错的关于Spring Cloud Zuul 相关用法的文章,基本包含常用的一些场景,另外附上实际项目中的熔断.打印请求日志和登录验证的实例. 原文地址:https://www.cnblogs.com/shihaiming/p/8489006.html ,作者:https://www.cnblogs.com/shihaiming/ 1.服务熔断 package com.ftk.hjs.zuul.server.hystrix; import com.alibaba.fastjson.JSO

spring @component的作用

1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)   @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理. 下面写这个是引入component的

Java 权限框架 Shiro 实战二:与spring集成、filter机制

Shiro和Spring的集成,涉及到很多相关的配置,涉及到shiro的filer机制以及它拥有的各种默认filter,涉及到shiro的权限判断标签,权限注解,涉及到session管理等等方面. 1. 配置 首先需要在web.xml中专门负责接入shiro的filter: <!-- shiro 安全过滤器 --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.

spring cloud zuul filter 设置过滤请求

利用spring cloud zuul 设置spring boot 中的filter ,出现了跨域问题,然后根据设置response可解决跨域,同时完成过滤请求 ********************************************************************************************************************* //主要执行的类public class PreRequestLogFilter extends Zu

spring字符编码filter

项目中的字符编码问题,spring提供统一的字符处理filter,只需要在项目入口web.xml中配置CharacterEncodingFilter即可,具体配置如下: <!-- 配置过滤器,同时转码所有请求为utf-8编码,解决乱码问题 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.fil

Spring @Component 注解的使用

使用说明 这个注解用于声明当前的类是一个组件类,Spring 会通过类路径扫描来自动侦测和自动装配这些组件,创建一个个 bean 后,注册到 Spring 容器中. 带 @Component 注解的类和自动创建的 bean 之间存在隐式的一对一映射关系.由于只需要声明一个注解,其他过程都是自动化的,所以对 bean 的创建过程可控程度较低. 该注解相当于: <bean id="useService" class="com.test.service.UserService

转!!spring @component 详解 默认初始化bean的名字 VNumberTask类 就是 VNumberTask

参考链接:信息来源 今天碰到一个问题,写了一个@Service的bean,类名大致为:CUser xml配置: <context:component-scan base-package="com.xxx.xx.x"/> 结果启动报错:No bean named 'cUser' is defined,即找不到名为cUser的bean bean的名字不是我预期的"cUser",临时将bean的名字硬性指定成了cUser来解决的,即:@Service(&quo