[Spring] Ioc-study

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.