當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring过滤器组件自动扫描
生活随笔
收集整理的這篇文章主要介紹了
Spring过滤器组件自动扫描
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在這個(gè)Spring自動(dòng)組件掃描的教程,您已經(jīng)了解如何使Spring自動(dòng)掃描您的組件。在這篇文章中,我們將展示如何使用組件過(guò)濾器自動(dòng)掃描過(guò)程。 1.過(guò)濾組件?- 包含 參見(jiàn)下面的例子中使用Spring?“過(guò)濾”?掃描并注冊(cè)匹配定義“regex”,即使該類(lèi)組件的名稱(chēng)未標(biāo)注?@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 過(guò)濾
<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>執(zhí)行
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] 在這個(gè)XML過(guò)濾中,所有文件的名稱(chēng)中包含 DAO 或?Service(*DAO.*,?*Services.*) 單詞將被檢測(cè)并在 Spring 容器中注冊(cè)。 2.過(guò)濾組件?-?不包含 另外,您還可以排除指定組件,以避免?Spring?檢測(cè)和?Spring?容器注冊(cè)。不包括在這些文件中標(biāo)注有?@Service?。 <context:component-scan base-package="com.yiibai.customer" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> 不包括那些包含DAO這個(gè)詞組文件名。 <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總結(jié)
以上是生活随笔為你收集整理的Spring过滤器组件自动扫描的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在上海,一个人的工资能承担房租吗?
- 下一篇: 老万空气能kD一90N1&#47