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的 ...
随机推荐
- VS2012界面改为英文
需要下载一个语言包 http://www.microsoft.com/en-us/download/confirmation.aspx?id=30681 还是不要指望这个,简直坑爹. 我把所有中文版的 ...
- oracle command - creata database dbca & create network netca Demo
#Creata database command: dbca [root@redhat4 ~]# su - oracle[oracle@redhat4 ~]$ dbca # ...
- poj 1067 取石子游戏( 威佐夫博奕)
题目:http://poj.org/problem?id=1067 题意:有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的 ...
- 一台电脑同时运行多个tomcat配置方法
当第一个tomcat启动后,后面tomcat的server.xml中的端口不管怎么改,仍然会报端口冲突.后来在dos下运行才发现所有的tomcat都会去找CATALINA_HOME和CATALINA_ ...
- HDU 4927 大数运算
模板很重要 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostrea ...
- Codeforces 379D - New Year Letter
原题地址:http://codeforces.com/contest/379/problem/D 题目大意:给出一种生成字符串的方法 s[k] = s[k - 2] + s[k - 1],其中加法意为 ...
- 在ASP.NET中如何判断用户IE浏览器的版本
f ( Request.Browser.MajorVersion == ) { // to do } ................................................. ...
- JS兼容性问题(FF与IE)
不同浏览器中js兼容问题大全 1.document.formName.item('itemName')问题 说明: //IE下(两种) document.formName.item("ite ...
- Lambda表达式【转】
lambda表达式是对匿名方法的一种改进,具有更加简洁的语法和更易理解的形式,lambda表达式可以包括表达式和语句,并且可以用与创建委托或表达式目录树类型. lambda表达式都使用Lambda运算 ...
- HTTP请求头host解析
Host: 域名 Host表示请求的服务器网址: request headers中的host字段 例如有user.xiaoqiang.com,hotel.xiaoqiang.com 现在需要登录后 ...