zoukankan      html  css  js  c++  java
  • [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测试结果:

  • 相关阅读:
    JAVA基础知识
    php中数据库服务器连接类库文件的编写
    面向对象三大特征之 封装
    composer 报错处理
    【Linux】ssh免密登录
    【天天数据结构和算法】PHP中trie数据结构的使用场景和代码实例
    【天天数据结构和算法】PHP实现二叉搜索树
    【天天数据结构和算法】PHP实现二分查找的两种方法
    【Linux】CentOS 7下yum成功安装 MySQL 5.7
    【实例】除法转移位运算
  • 原文地址:https://www.cnblogs.com/HD/p/3962541.html
Copyright © 2011-2022 走看看