在这个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>
 

Spring过滤器组件自动扫描的更多相关文章

  1. Spring重温(四)--Spring自动组件扫描

    通常情况下,声明所有的Bean类或组件的XML bean配置文件,这样Spring容器可以检测并注册Bean类或组件. 其实,Spring是能够自动扫描,检测和预定义的项目包并实例化bean,不再有繁 ...

  2. spring过滤器

    什么是过滤器 Spring 中不能处理用户请求,但可以用来提供过滤作用的一种Servlet规范.在请求进入容器之后,还未进入Servlet之前进行预处理,并且在请求结束返回给前端这之间进行后期处理.具 ...

  3. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

  4. spring 过滤器简介

    spring 过滤器简介 过滤器放在容器结构的什么位置 过滤器放在web资源之前,可以在请求抵达它所应用的web资源(可以是一个Servlet.一个Jsp页面,甚至是一个HTML页面)之前截获进入的请 ...

  5. Spring Framework 组件注册 之 @Import

    Spring Framework 组件注册 之 @Import 写在前面 向spring中注册组件或者叫javaBean是使用spring的功能的前提条件.而且spring也提供了很多种方式,让我们可 ...

  6. Spring Framework 组件注册 之 @Component

    Spring Framework 组件注册 之 @Component 写在前面 在spring大行其道的今天,对于spring的使用和掌握乃是不可缺少的必备技能.但是spring的整个体系尤为庞大,对 ...

  7. Spring Framework 组件注册 之 FactoryBean

    Spring Framework 组件注册 之 FactoryBean 前言 前两篇文章介绍了如何使用@Component,@Import注解来向spring容器中注册组件(javaBean),本文将 ...

  8. 跟我学SpringCloud | 第十九章:Spring Cloud 组件 Docker 化

    前面的文章<跟我学SpringCloud | 第十八篇:微服务 Docker 化之基础环境>我们介绍了基础环境系统和 JRE 的容器化,这一节我们介绍 Spring Cloud 组件的容器 ...

  9. SpringBoot学习(二)—— springboot快速整合spring security组件

    Spring Security 简介 spring security的核心功能为认证(Authentication),授权(Authorization),即认证用户是否能访问该系统,和授权用户可以在系 ...

随机推荐

  1. AJP与HTTP比较和分析

    系统环境: OS:Ubuntu 10.10 (2G) Servlet Container:tomcat-tomcat-7.0.23  (最大内存:default 256M  maxThreads:50 ...

  2. 关于boost 的smart_ptr 的使用问题

    boost 的smart_ptr 库中含有好几种智能指针,大家用的最多的应该是shared_ptr ,为啥呢?好用,不用管他啥时候会自动删除等等,而且拷贝和复制都很到位, 但实际上,这个库也有问题,连 ...

  3. python模块之xml.etree.ElementTree

    xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...

  4. rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library

    使用yum安装出现问题:rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library 解决 ...

  5. Python+Selenium 自动化实现实例-单元测试报告

    代码如下: # -*- coding: utf-8 -*- from selenium import webdriver import unittest,time import HTMLTestRun ...

  6. normalize.css阅读笔记

    最近在被各种浏览器的CSS兼容折磨,所以看了看normalize的源代码来了解一些常见的浏览器间不一致的CSS渲染问题…… 源代码在这里 text-size-adjust 用法参见Apple的文档和M ...

  7. grep 同时排除多个关键字

    不说废话, 例如需要排除 abc.txt 中的  mmm   nnn grep -v 'mmm\|nnn' abc.txt 再举个例子,需要确定mac 的本机ip地址,  显然直接可以输入 ifcon ...

  8. 将 sublime test3 添加到鼠标右键的方法

    将 sublime test3 添加到鼠标右键的方法 将 sublime_addright.inf 文件放到 sublime text 安装目录进行安装即可. 文件名:sublime_addright ...

  9. Java SHAA加密

    package com.util; import java.security.MessageDigest; /** * 采用SHAA加密 */ public class SHAUtil { /*** ...

  10. 洛谷P3857 [TJOI2008]彩灯 [线性基]

    题目传送门 彩灯 题目描述 Peter女朋友的生日快到了,他亲自设计了一组彩灯,想给女朋友一个惊喜.已知一组彩灯是由一排N个独立的灯泡构成的,并且有M个开关控制它们.从数学的角度看,这一排彩灯的任何一 ...