Spring过滤器组件自动扫描
2024-09-20 17:55:43
在这个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>
最新文章
- Ctrip Mydream
- WPF——传实体类
- ASP.NET实现从服务器下载文件2
- 从ACM中删除一个已经创建的Library
- 以程序的方式操纵NTFS的文件权限(陈皓)
- 性能超越 Redis 的 NoSQL 数据库 SSDB
- 《Java虚拟机原理图解》1.3、class文件里的訪问标志、类索引、父类索引、接口索引集合
- 跨域CORS
- Spark环境搭建(下)——Spark安装
- JAVA中抽象类的使用
- 【java集合框架源码剖析系列】java源码剖析之LinkedList
- django分页功能实现
- Let me introduce myself
- 第18月第22天 机器学习first
- Java对象的内存实际占用
- 安卓AlertDialog 的使用
- Linux系统各发行版镜像下载(借阅)
- Oracle 视图view
- mui --- 怎么获取百度地图定位功能
- 61. oracle给用户解锁
热门文章
- nginx 各种配置
- maven将jar包打如本地仓库命令
- csu 1555(线段树经典插队模型-根据逆序数还原序列)
- csu 1552(米勒拉宾素数测试+二分图匹配)
- 【LOJ】#2131. 「NOI2015」寿司晚宴
- lr_start_transaction/lr_end_transaction事物组合
- Java 深入浅出String
- PHP包管理
- 使用 Eigen 3.3.3 进行矩阵运算
- C# 找出实现某个接口的所有类