XML configuration
Procedure: Set automatic assembly of the package--using Include-filter filter type to select the Regex as regular expression-->expression is the expression is the limit condition
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd6http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">7<!--for automatic assembly--8<context:component-scan base- Package= "Cn.lonecloud" >9<!--contains the DAO layer type type under a package the Regex represents the restriction conditions that the regular expression needs to set--Ten<!--some of the DAO layers under this expression that use this representation can be directly used without annotations-- One<context:include-filter type= "regex" expression= "cn.lonecloud.dao.*dao.*"/> A<context:include-filter type= "regex" expression= "cn.lonecloud.service.*service.*"/> -<!--classes that are configured under this type will not be registered with the spring container- -<context:exclude-filter type= "regex" expression= "Cn.lonecloud.service.TestService"/> the</context:component-scan> - -</beans>
DAO layer
1 Package Cn.lonecloud.dao; 2 3 Public class Testdao {4 Public void Test01 () {5 System.out.println ("Dao"); 6 }7 }
Service Layer
1 PackageCn.lonecloud.service;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 5 ImportCn.lonecloud.dao.TestDao;6 7 Public classTestservice {8 9 @AutowiredTen Testdao Testdao; One A Public voidTest01 () { - testdao.test01 (); - } the}
Test layer
1 Packagecn.lonecloud.test;2 3 ImportOrg.junit.Before;4 Importorg.junit.Test;5 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;6 7 ImportCn.lonecloud.service.TestService;8 9 Public classTestmain {TenClasspathxmlapplicationcontext xmlapplicationcontext=NULL; One @Before A Public voidinit () { -xmlapplicationcontext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); - } the @Test - Public voidTest01 () { -Testservice Testservice=xmlapplicationcontext.getbean (Testservice.class); - testservice.test01 (); + } -}
Spring implementation without annotations for automatic injection