當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring过滤器组件自动扫描
生活随笔
收集整理的這篇文章主要介紹了
Spring过滤器组件自动扫描
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在這個Spring自動組件掃描的教程,您已經了解如何使Spring自動掃描您的組件。在這篇文章中,我們將展示如何使用組件過濾器自動掃描過程。 1.過濾組件?- 包含 參見下面的例子中使用Spring?“過濾”?掃描并注冊匹配定義“regex”,即使該類組件的名稱未標注?@Component?。
DAO 層
package com.yiibai.customer.dao;public class CustomerDAO {@Overridepublic 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 {@AutowiredCustomerDAO customerDAO;@Overridepublic 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://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> 下載代碼 –?http://pan.baidu.com/s/1pKnzB2F總結
以上是生活随笔為你收集整理的Spring过滤器组件自动扫描的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在上海,一个人的工资能承担房租吗?
- 下一篇: Spring自动装配Beans