Spring Filter components in auto scanning
In this Spring auto component scanning tutorial, you learn about how to make Spring auto scan your components. In this article, we show you how to do component filter in auto scanning process.
1. Filter component – include
See following example to use Spring “filtering
” to scan and register components’ name which matched defined “regex
”, even the class is not annotated with @Component
.
DAO layer
package com.mkyong.customer.dao;
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
Service layer
package com.mkyong.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import com.mkyong.customer.dao.CustomerDAO;
public class CustomerService
{
@Autowired
CustomerDAO customerDAO;
@Override
public String toString() {
return "CustomerService [customerDAO=" + customerDAO + "]";
}
}
Spring filtering.
<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.mkyong" >
<context:include-filter type="regex"
expression="com.mkyong.customer.dao.*DAO.*" />
<context:include-filter type="regex"
expression="com.mkyong.customer.services.*Service.*" />
</context:component-scan>
</beans>
Run it
package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.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);
}
}
Output
CustomerService [customerDAO=Hello , This is CustomerDAO]
In this XML filtering, all files’s name contains DAO
or Service
(*DAO.*
, *Services.*
) word will be detect and register in Spring container.
2. Filter component – exclude
On the other hand, you can also exclude specified components, to avoid Spring to detect and register it in Spring container.
Exclude those files annotated with @Service
.
<context:component-scan base-package="com.mkyong.customer" >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
Exclude those files name contains DAO word.
<context:component-scan base-package="com.mkyong" >
<context:exclude-filter type="regex"
expression="com.mkyong.customer.dao.*DAO.*" />
</context:component-scan>
Spring Filter components in auto scanning的更多相关文章
- Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件
一. Spring Auto Scanning Components —— 自动扫描组件 1. Declares Components Manually——手动配置componen ...
- Spring Auto scanning components
Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...
- spring filter过滤器
1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...
- Spring Filter过滤表单中的非法字符
使用Spring Filter过滤表单中的非法字符 1 package test.filter; 2 3 import java.io.IOException; 4 import java.util. ...
- spring filter拦截器
实现的功能:判断用户是否已登录,未登录用户禁止访问任何页面或action,自动跳转到登录页面.比较好的做法是不管什么人都不能直接访问jsp页面,要访问就通过action,这样就变成了一个实实在在的权限 ...
- web.xml 文件中一般包括 servlet, spring, filter, listenr的配置的加载顺序
首先可以肯定 加载顺序与他们在web.xml 文件中的先后顺序无关. web.xml 中 listener 和 serverlet 的加载顺序为 先 listener 后serverlet最终得出结果 ...
- spring filter lister servlet
https://blog.csdn.net/nacey5201/article/details/8547772 https://blog.csdn.net/xwl617756974/article/d ...
- Spring Filter过滤器,Spring拦截未登录用户权限限制
转载自:http://pouyang.iteye.com/blog/695429 实现的功能:判断用户是否已登录,未登录用户禁止访问任何页面或action,自动跳转到登录页面. 比较好的做法是不管什 ...
- Spring filter和拦截器(Interceptor)的区别和执行顺序
转载自:http://listenup.iteye.com/blog/1559553 1.Filter过滤器只过滤jsp文件不过滤action请求解决方案 解决办法:在web.xml中将filter的 ...
随机推荐
- openfire中mysql的前期设置
使用openfire的时候如果需要使用自己的mysql数据库,需要提前进行设置,下面将记录下,基本的设置过程. 一.前期准备工作: 1.先下载两个工具一个是mysql数据库还有一个是SQLyog(可以 ...
- POJ3485 区间问题
题目描述有些坑.. 题意: 有一条高速公路在x轴上,从(0,0)到(L,0).周围有一些村庄,希望能够在高速公路上开通几个出口,使得每个村庄到最近的出口距离小于D,求出最少需要开通多少个出口. 解题思 ...
- poj3307
可以证明,每个符合的数都由2,3,5,7相乘得到. 依据猜想:下一个出现的数是由前面某个数乘上这几个数之一得到的新的数. 假设之前的数均满足序列,则因为下一个数必有2,3,5,7相乘得到,而这个数之前 ...
- Codeforces Round #273 (Div. 2)
A. Initial Bet 题意:给出5个数,判断它们的和是否为5的倍数,注意和为0的情况 #include<iostream> #include<cstdio> #incl ...
- WebView线性进度条
参考:http://www.cnblogs.com/hubli/p/4835549.html APP会跳转网页时候,请参考:http://blog.csdn.net/raphael55/article ...
- I.MX6 linux Qt 同时支持Touch、mouse
/***************************************************************************** * I.MX6 linux Qt 同时支持 ...
- 《C++ Primer 4th》读书笔记 第7章-函数
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3912413.html
- <九>面向对象分析之UML核心元素之设计类,类,属性,方法,可见性
设计类
- 【转】No JVM could be found on your system解决方法
原文网址:http://my.oschina.net/liusicong/blog/324964 在安装android studio时,报错: Error launching android Stud ...
- 常见的js函数
改变元素的样式 var changeStyle = function(elem,name,value){ elem.style[name] = value; } 空位补零 fu ...