dao 层
package com.h3.customer.dao; public class customerdao { @override public string tostring() { return "hello , this is customerdao"; } }
service 层
package com.h3.customer.services; import org.springframework.beans.factory.annotation.autowired; import com.h3.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.h3" > <context:include-filter type="regex" expression="com.h3.customer.dao.*dao.*" /> <context:include-filter type="regex" expression="com.h3.customer.services.*service.*" /> </context:component-scan> </beans>
执行
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.h3.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.h3.customer" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.service" /> </context:component-scan>
<context:component-scan base-package="com.h3" > <context:exclude-filter type="regex" expression="com.h3.customer.dao.*dao.*" /> </context:component-scan>