在这个Spring自动组件扫描的教程,您已经了解如何使Spring自动扫描您的组件。在这篇文章中,我们将展示如何使用组件过滤器自动扫描过程。
1.过滤组件 - 包含
参见下面的例子中使用Spring “过滤” 扫描并注册匹配定义“regex”,即使该类组件的名称未标注 @Component 。

DAO 层

package com.yiibai.customer.dao;public class CustomerDAO
{@Overridepublic String toString() {return "Hello , This is CustomerDAO";}
}

Service 层

package com.yiibai.customer.services;import org.springframework.beans.factory.annotation.Autowired;
import com.yiibai.customer.dao.CustomerDAO;public class CustomerService
{@AutowiredCustomerDAO customerDAO;@Overridepublic String toString() {return "CustomerService [customerDAO=" + customerDAO + "]";}}

Spring 过滤

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.yiibai" ><context:include-filter type="regex" expression="com.yiibai.customer.dao.*DAO.*" /><context:include-filter type="regex" expression="com.yiibai.customer.services.*Service.*" /></context:component-scan></beans>

执行

package com.yiibai.common;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.customer.services.CustomerService;public class App
{public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});CustomerService cust = (CustomerService)context.getBean("customerService");System.out.println(cust);}
}

输出

CustomerService [customerDAO=Hello , This is CustomerDAO]
在这个XML过滤中,所有文件的名称中包含 DAO 或 Service(*DAO.*, *Services.*) 单词将被检测并在 Spring 容器中注册。
2.过滤组件 - 不包含
另外,您还可以排除指定组件,以避免 Spring 检测和 Spring 容器注册。不包括在这些文件中标注有 @Service 。
<context:component-scan base-package="com.yiibai.customer" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />      </context:component-scan>
不包括那些包含DAO这个词组文件名。
<context:component-scan base-package="com.yiibai" ><context:exclude-filter type="regex" expression="com.yiibai.customer.dao.*DAO.*" />      </context:component-scan>
下载代码 – http://pan.baidu.com/s/1pKnzB2F

Spring过滤器组件自动扫描相关推荐

  1. Spring Aop 组件概述

    Spring Aop 概述 AOP(Aspect-Oriented Programming) 面向切面编程, 这种编程模型是在 OOP(Object-Oriented Programming) 的基础 ...

  2. spring 组件扫描_避免不必要的Spring配置组件扫描

    spring 组件扫描 我在堆栈溢出中遇到了一个有趣的问题. Brett Ryan有问题,Spring Security配置被初始化了两次. 当我查看他的代码时,我发现了问题所在. 让我展示显示代码. ...

  3. 避免不必要的Spring配置组件扫描

    我在堆栈溢出中遇到了一个有趣的问题. Brett Ryan有问题,Spring Security配置被初始化了两次. 当我查看他的代码时,我发现了问题所在. 让我展示显示代码. 他有相当标准的Spri ...

  4. spring过滤器Filter 、 拦截器Interceptor 、 切片Aspect 详解

    springboot 过滤器Filter vs 拦截器Interceptor vs 切片Aspect 详解 1 前言 最近接触到了过滤器和拦截器,网上查了查资料,这里记录一下,这篇文章就来仔细剖析下过 ...

  5. spring aop组件_安全性中的Spring AOP –通过方面控制UI组件的创建

    spring aop组件 以下文章将显示在我参与的一个项目中,我们如何使用Spring的AOP来介绍一些与安全性相关的功能. 这样的概念是,为了使用户能够看到某些UI组件,他需要具有一定级别的安全特权 ...

  6. 分布式锁 动态代理 Java数据结构List,Set,Map,Spring执行流程,Spring MVC组件

    这里对今日的内容进行总结: 分布式锁具备的条件: 具备的条件: 在分布式系统环境下,一个方法在同一时间只能被一个机器的一个线程执行. 高可用的获取锁与释放锁. 高性能的获取锁与释放锁. 具备可重入的特 ...

  7. spring boot组件_Spring Boot Framework的关键组件和内部

    spring boot组件 In my previous post "Introduction to Spring Boot", we have discussed about S ...

  8. java敏感词过滤器组件

    简介:用java写过滤器组件,需求:这里可以☆赌☆博☆,可以☆嫖☆娼☆,可以☆吸☆毒☆,可以☆开☆票☆,哈哈哈!经过过滤器过滤后变为这里可以☆*☆,可以☆***☆,可以☆***☆,可以☆*☆,哈哈哈! ...

  9. Spring 条件组件注解:`@Conditional` 与 `@ConditionalOnBean`

    Spring 条件组件注解:@Conditional 与 @ConditionalOnBean 文章目录 Spring 条件组件注解:`@Conditional` 与 `@ConditionalOnB ...

最新文章

  1. 陌陌安全开源了 Java 静态代码安全审计插件
  2. 智办事协同办公:数字化转型下的高效工作方式
  3. 24.两两交换链表中的结点
  4. 不一样的随机数生成方法(C/C++)
  5. mysql事务实战_MySQL - 实战 (2) - 事务隔离
  6. MFC 自定义消息四步曲与在参数中传递变量
  7. php枚举mysql,小技巧:取得MYSQL中ENUM(枚举)列的全部可能值。-PHP教程,PHP应用...
  8. 一进庙会freeeim
  9. 使用Notepad++正则提取数据,然后进行替换
  10. FusionChart 保存图片
  11. 于仕琪老师的人脸检测库
  12. 代理ip,代理服务器等相关
  13. 如何利用Python对服务器的接口进行压力测试
  14. Apache ShenYu源码阅读系列-Dubbo插件
  15. 计算机网络长度,以太网中mtu默认长度 你应该知道的计算机网络知识(2)
  16. Artifact “xxx - xxxx“:war exploded: 部署工件时出错。请参阅服务器日志了解详细
  17. hutool工具类生成二维码案例
  18. 计算机开机无法进入桌面,电脑开机不能进入系统界面的故障修复
  19. 最常见的卸载Mac应用软件方法,彻底清除,果断收藏!
  20. (11)EKF - (3) EKF3匹配度和Lane切换

热门文章

  1. Qt Creator建立多个平台
  2. Qt Creator从Maya导出3D资产
  3. 有序数组中查找数字的范围
  4. c++多态相关面试题
  5. C语言中时间的基本用法
  6. C语言fstat函数获取文件的大小
  7. mysql 优化 类型_MySQL数据类型的优化选择
  8. 10_InfluxDB常用函数(三)变换类函数(DERIVATIVE, DIFFERENCE,ELAPSED,MOVING_AVERAGE,NON_NEGATIVE_DERIVATIVE)等
  9. 53_Auto-Encoders和Variational AutoEncoders(VAE)、PCA降维和Auto-Encoders降维的比较、Auto-Encoders变种、pytorch实现等
  10. 数据库元数据数据字典查询_9_列出给定表的所有约束