• [Spring] IOC


    Spring IOC简单注入例子,本例子使用JUnit进行测试。Spring版本:3.2


    项目结构:

    Spring所需引用的JAR包:


    Spring XML配置:

    springContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="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">
        <import resource="dao.xml"/>
        <import resource="service.xml"/>
    </beans>

    dao.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="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">
        <bean name="accountDaoFactory" class="com.my.dao.AccountFactory" abstract="true"></bean>
        <bean name="accountDAO" class="com.my.dao.mysql.AccountDAO" parent="accountDaoFactory"></bean>
    </beans>

    service.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="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">
        <bean name="accountServiceFactory" class="com.my.service.AccountServiceFactory" abstract="true"></bean>
        <bean name="accountService" class="com.my.service.mysql.AccountService" parent="accountServiceFactory">
            <property name="account" ref="accountDAO"></property>
        </bean>
    </beans>

    DAO的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 AccountDAO extends com.my.dao.AccountFactory {
        /*
         * Find account by account id
         */
        @Override
        public Integer findAccount(Integer accountID) {
            return accountID;
        }
    }

    Service的java class:

    package com.my.service;
    
    public abstract class AccountServiceFactory {
        protected com.my.dao.AccountFactory account;
    
        public com.my.dao.AccountFactory getAccount() {
            return account;
        }
    
        public void setAccount(com.my.dao.AccountFactory account) {
            this.account = account;
        }
        
        /*
         * Find account by id
         */
        public abstract Integer finAccount(Integer accountID);
    }
    package com.my.service.mysql;
    
    public class AccountService extends com.my.service.AccountServiceFactory {
        @Override
        public Integer finAccount(Integer accountID) {
            return account.findAccount(accountID);
        }
    }

    测试类:

    package com.my.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.my.service.AccountServiceFactory;
    
    public class SpringTest {
        private ApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml");
        
        public SpringTest(){
        }
        
        public Integer findAccount(Integer accountID){
            AccountServiceFactory account = (AccountServiceFactory)context.getBean("accountService");
            Integer id = account.finAccount(100);
            return id;
        }
    }

    JUnit测试:

    package com.my.test;
    
    import static org.junit.Assert.*;
    
    import org.junit.Before;
    import org.junit.Test;
    
    public class SpringTestTest {
    
        private SpringTest sp = new SpringTest();
    
        @Before
        public void setUp() throws Exception {
        }
    
        @Test
        public void testFindAccount() {
            Integer accountID = 100;
            Integer result = sp.findAccount(accountID);
            assertEquals((Integer)100, result);
            System.out.println(result);
        }
    }

    运行JUnit测试结果:

  • 相关阅读:
    Lightoj 1082【RMQ】
    hrbust1444 逃脱 【BFS】
    萌新学习笔记之哈夫曼树
    lightoj 1085【离散化+树状数组】
    CodeForces 586D【BFS】
    lightoj 1089 【离散化+线段树】
    lightoj 1088【树状数组+离散化】
    《算法导论》笔记 第6章 6.2保持堆的性质
    《算法导论》笔记 第6章 6.1堆
    【python】__all__
  • 原文地址:https://www.cnblogs.com/HD/p/3962541.html
Copyright © 2020-2023  润新知