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测试结果:

  • 相关阅读:
    android 的权限
    做android遇到有问题有感
    帮人写的 论文 C语言的 学生管理系统
    android 服务器的 mysql 查询悲剧
    android开发遇到的问题
    想和各位技术高人材交流技术特建了相关的QQ群
    Invalid token '44' in input string
    设置PLSQL Developer访问本机64位Oracle
    SQL Server 2005还原数据库时出现“不能选择文件或文件组XXX_log用于此操作……错误:3219……”的解决方法
    C#的JSON开发包 LitJSON
  • 原文地址:https://www.cnblogs.com/HD/p/3962541.html
Copyright © 2011-2022 走看看