Spring Filter components in auto scanning

本文将指导您如何在Spring中实现组件自动扫描,并展示如何使用过滤器来筛选匹配特定规则的组件,包括如何包含或排除指定的组件。

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>

转载于:https://www.cnblogs.com/ghgyj/p/4749814.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值