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.tw511.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]
<context:component-scan base-package="com.yiibai.customer" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
<context:component-scan base-package="com.yiibai" > <context:exclude-filter type="regex" expression="com.yiibai.customer.dao.*DAO.*" /> </context:component-scan>