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. 通知(Notification) 、 应用间通信(一)

    1 使用通知中心发送消息 1.1 问题 当一个对象需要向多个接受者发送消息的,或者不用知道消息的接收者是谁,就可以使用IOS提供的NSNotificationCenter通知中心,本案例使NSNoti ...

  2. js之阻止事件冒泡(待修改)和阻止默认事件

    阻止默认事件(event.stopPropagation()): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  3. jquery 平滑滚动页面到某个锚点

    $(document).ready(function() {         $("a.topLink").click(function() {                 $ ...

  4. shell 脚本实战笔记(1)--source/fork/exec的区别

    在linux shell编程中, 随着脚本处理规模的变大, 会嵌套调用子进程. 但父进程以不同的方式调用子进程, 其结果和背后的机制不尽相同.这边就简单描述下source/fork/exec调用子进程 ...

  5. js图片无缝滚动代码

    想必大家都注意到<marquee>的不循环滚动,所以出现了很多替代脚本,或iframe或JS输出<marquee>,不管怎么做,都略显麻烦.下面说一下这个相对简单的实现思路:一 ...

  6. 让超链接点击后不跳转,可以用href = "#",但是这个#就会锚点到页面最上边 点击链接后不跳转可以设置成

    让超链接点击后不跳转,可以用href = "#",但是这个#就会锚点到页面最上边     点击链接后不跳转可以设置成     1.<a href="javascri ...

  7. Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明

    Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明   开发环境:STM32F103RB(128K Flash 20K RAM)+MDK3.50+JLINK V7(v ...

  8. 数字字符与金钱RMB之间的转换

    FormatMoney()  函数,直接将一个数字字符串,转化为 万元,并且格式化小数点保留两位   如右->¥(元.角.分) sprintf("%.2f", $value) ...

  9. MySql中的skip-name-resovle

    mysql用的一直很好用,有一次断网了,发现连接虚拟机里的mysql特别费劲,几经扔腾,才知道是因为断网以后,名字解析这块有点问题,在my.cnf文件中加了一条skip-name-resovle,果断 ...

  10. Integer.parseInt()和Integer.valueOf()有什么区别

    jdk的源代码的时候注意到Integer.parseInt(s) 和 Integer.valueOf(s)的具体代码的实现有所区别: Java代码 public static int parseInt ...