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

[Spring] IOC - study的更多相关文章

  1. Spring IoC源码解析——Bean的创建和初始化

    Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器 ...

  2. 框架源码系列六:Spring源码学习之Spring IOC源码学习

    Spring 源码学习过程: 一.搞明白IOC能做什么,是怎么做的  1. 搞明白IOC能做什么? IOC是用为用户创建.管理实例对象的.用户需要实例对象时只需要向IOC容器获取就行了,不用自己去创建 ...

  3. 框架:Spring IoC

    Spring篇 第二章.Spring IoC简介 一.基本概念 控制反转是一个比较抽象的概念,是Spring框架的核心,用来消减计算机程序的耦合问题. 依赖注入是IoC的另外一种说法,只是从不同的角度 ...

  4. Spring IOC 源码简单分析 04 - bean的初始化

      ### 准备 ## 目标 了解 Spring 如何初始化 bean 实例 ##测试代码 gordon.study.spring.ioc.IOC04_Initialization.java publ ...

  5. Spring IOC 源码简单分析 03 - 循环引用

    ### 准备 ## 目标 了解 Spring 如何处理循环引用 ##测试代码 gordon.study.spring.ioc.IOC03_CircularReference.java   ioc03. ...

  6. Spring IOC 源码简单分析 02 - Bean Reference

    ### 准备 ## 目标 了解 bean reference 装配的流程 ##测试代码 gordon.study.spring.ioc.IOC02_BeanReference.java   ioc02 ...

  7. Spring IOC 源码简单分析 01 - BeanFactory

    ### 准备 ## 目标 了解 Spring IOC 的基础流程 ## 相关资源 Offical Doc:http://docs.spring.io/spring/docs/4.3.9.RELEASE ...

  8. 【初探Spring】------Spring IOC(三):初始化过程---Resource定位

    我们知道Spring的IoC起到了一个容器的作用,其中装得都是各种各样的Bean.同时在我们刚刚开始学习Spring的时候都是通过xml文件来定义Bean,Spring会某种方式加载这些xml文件,然 ...

  9. 【初探Spring】------Spring IOC(一)

    IOC:Inversion of Control(控制反转).IOC它所体现的并不是一种技术,而是一种思想,一种将设计好的对象交给容器来管理的思想.IOC的核心思想就体现在控制.反转这两个词上面,要理 ...

随机推荐

  1. Qt的QLineEdit显示密码

    ui->lineEdit->setEchoMode( QLineEdit::Password );

  2. 【题解】【数组】【Leetcode】Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  3. Open vSwitch 给虚拟机网卡限流(QoS)

    这里我们简单描述下如何通过Open vSwitch给虚拟机限流(出流量),同时测试限流效果.测试环境继续复用<整合Open vSwitch与DNSmasq为虚拟机提供DHCP功能>一文中描 ...

  4. Azure SQL 数据库弹性池现已面市

    我们高兴地宣布Azure SQL 数据库弹性池现已正式面市.弹性池自去年试运行以来,得到许多软件即服务(SaaS)供应商和企业开发人员的认可,他们利用弹性池管理持续增长的云数据库和应用程序,成果高效. ...

  5. 转 SQL Server中关于的checkpoint使用说明

    在SQL Server中有一个非常重要的命令就是CheckPoint,它主要作用是把缓存中的数据写入mdf文件中. 其实在我们进行insert, update, delete时,数据并没有直接写入数据 ...

  6. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. sublime text3 本地化

    博客園中搜到n篇 同題日誌 沒一篇靠譜 超級反感這種行為,浪費別人的時間無異於謀財害命 自動: Control+` (注意不是cmd+p) import urllib.request,os,hashl ...

  8. javascript判断浏览器的版本

    在javascript中直接的使用navigator.userAgent就可以获取当前浏览器的版本等信息,以下是列出来的关于不同浏览器显示的值(Windows.Android.iPhone): IE6 ...

  9. My97DatePickerBeta 时间选择控件用法

    用法说明:只需要调用一个js<script language="javascript" type="text/javascript" src=" ...

  10. 关于Toad连接DB2的sqlstate=08001错误

    新装的centos6.3+db29.7,数据库导入完了的之后用Toad连接访问之的时候出错了. DB2 Database Error: ERROR [08001] [IBM] SQL30081N A ...