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

DAO 层

package com.yiibai.customer.dao;

public class CustomerDAO
{
@Override
public 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
{
@Autowired
CustomerDAO customerDAO; @Override
public 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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://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>
 

最新文章

  1. Ctrip Mydream
  2. WPF——传实体类
  3. ASP.NET实现从服务器下载文件2
  4. 从ACM中删除一个已经创建的Library
  5. 以程序的方式操纵NTFS的文件权限(陈皓)
  6. 性能超越 Redis 的 NoSQL 数据库 SSDB
  7. 《Java虚拟机原理图解》1.3、class文件里的訪问标志、类索引、父类索引、接口索引集合
  8. 跨域CORS
  9. Spark环境搭建(下)——Spark安装
  10. JAVA中抽象类的使用
  11. 【java集合框架源码剖析系列】java源码剖析之LinkedList
  12. django分页功能实现
  13. Let me introduce myself
  14. 第18月第22天 机器学习first
  15. Java对象的内存实际占用
  16. 安卓AlertDialog 的使用
  17. Linux系统各发行版镜像下载(借阅)
  18. Oracle 视图view
  19. mui --- 怎么获取百度地图定位功能
  20. 61. oracle给用户解锁

热门文章

  1. nginx 各种配置
  2. maven将jar包打如本地仓库命令
  3. csu 1555(线段树经典插队模型-根据逆序数还原序列)
  4. csu 1552(米勒拉宾素数测试+二分图匹配)
  5. 【LOJ】#2131. 「NOI2015」寿司晚宴
  6. lr_start_transaction/lr_end_transaction事物组合
  7. Java 深入浅出String
  8. PHP包管理
  9. 使用 Eigen 3.3.3 进行矩阵运算
  10. C# 找出实现某个接口的所有类