Spring IOC Simple Injection example, this example uses JUnit for testing. Spring Version: 3.2
Project structure:
The jar package that spring needs to reference:
Spring XML configuration:
Springcontext.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <ImportResource= "Dao.xml"/> <ImportResource= "Service.xml"/></Beans>
Dao.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <Beanname= "Accountdaofactory"class= "Com.my.dao.AccountFactory"Abstract= "true"></Bean> <Beanname= "Accountdao"class= "Com.my.dao.mysql.AccountDAO"Parent= "Accountdaofactory"></Bean></Beans>
Service.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <Beanname= "Accountservicefactory"class= "Com.my.service.AccountServiceFactory"Abstract= "true"></Bean> <Beanname= "Accountservice"class= "Com.my.service.mysql.AccountService"Parent= "Accountservicefactory"> < Propertyname= "Account"ref= "Accountdao"></ Property> </Bean></Beans>
DAO's Java class:
package Com.my.dao; public abstract class Accountfactory { * Fin account by ID */ public abstract integer findaccount (integer AccountID);}
Package Com.my.dao.mysql; Public class extends com.my.dao.AccountFactory { / * * Findaccount by Account ID* / @Override public integer findaccount (integer accountID) { return AccountID;} }
Java Class for service:
PackageCom.my.service; Public Abstract classAccountservicefactory {protectedcom.my.dao.AccountFactory account; Publiccom.my.dao.AccountFactory Getaccount () {returnAccount ; } Public voidSetaccount (com.my.dao.AccountFactory account) { This. Account =Account ; } /** Find account by ID*/ Public Abstractinteger finaccount (integer AccountID);}
Package Com.my.service.mysql; Public class extends com.my.service.AccountServiceFactory { @Override public integer finaccount (integer AccountID) { return account.findaccount (AccountID); }}
Test class:
Packagecom.my.test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importcom.my.service.AccountServiceFactory; Public classSpringtest {PrivateApplicationContext context =NewClasspathxmlapplicationcontext ("Springcontext.xml"); Publicspringtest () {} Publicinteger findaccount (integer accountID) {accountservicefactory account= (accountservicefactory) context.getbean ("Accountservice"); Integer ID= Account.finaccount (100); returnID; }}
JUnit Test:
Packagecom.my.test;Import Staticorg.junit.assert.*;ImportOrg.junit.Before;Importorg.junit.Test; Public classSpringtesttest {PrivateSpringtest SP =Newspringtest (); @Before Public voidSetUp ()throwsException {} @Test Public voidTestfindaccount () {Integer AccountID= 100; Integer result=Sp.findaccount (AccountID); Assertequals ((Integer)100, result); SYSTEM.OUT.PRINTLN (result); }}
Run JUnit test results:
[Spring] Ioc-study