[Spring] IOC - study
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的更多相关文章
- Spring IoC源码解析——Bean的创建和初始化
Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器 ...
- 框架源码系列六:Spring源码学习之Spring IOC源码学习
Spring 源码学习过程: 一.搞明白IOC能做什么,是怎么做的 1. 搞明白IOC能做什么? IOC是用为用户创建.管理实例对象的.用户需要实例对象时只需要向IOC容器获取就行了,不用自己去创建 ...
- 框架:Spring IoC
Spring篇 第二章.Spring IoC简介 一.基本概念 控制反转是一个比较抽象的概念,是Spring框架的核心,用来消减计算机程序的耦合问题. 依赖注入是IoC的另外一种说法,只是从不同的角度 ...
- Spring IOC 源码简单分析 04 - bean的初始化
### 准备 ## 目标 了解 Spring 如何初始化 bean 实例 ##测试代码 gordon.study.spring.ioc.IOC04_Initialization.java publ ...
- Spring IOC 源码简单分析 03 - 循环引用
### 准备 ## 目标 了解 Spring 如何处理循环引用 ##测试代码 gordon.study.spring.ioc.IOC03_CircularReference.java ioc03. ...
- Spring IOC 源码简单分析 02 - Bean Reference
### 准备 ## 目标 了解 bean reference 装配的流程 ##测试代码 gordon.study.spring.ioc.IOC02_BeanReference.java ioc02 ...
- Spring IOC 源码简单分析 01 - BeanFactory
### 准备 ## 目标 了解 Spring IOC 的基础流程 ## 相关资源 Offical Doc:http://docs.spring.io/spring/docs/4.3.9.RELEASE ...
- 【初探Spring】------Spring IOC(三):初始化过程---Resource定位
我们知道Spring的IoC起到了一个容器的作用,其中装得都是各种各样的Bean.同时在我们刚刚开始学习Spring的时候都是通过xml文件来定义Bean,Spring会某种方式加载这些xml文件,然 ...
- 【初探Spring】------Spring IOC(一)
IOC:Inversion of Control(控制反转).IOC它所体现的并不是一种技术,而是一种思想,一种将设计好的对象交给容器来管理的思想.IOC的核心思想就体现在控制.反转这两个词上面,要理 ...
随机推荐
- Android 学习第1课,JDK工具包的安装
1. jdk 的下载 目前为止,jdk的最新版是jdk8,http://www.oracle.com/technetwork/java/javase/downloads/index.html 到这里下 ...
- ARM Mysql c 通信
/************************************************************************************ * ARM Mysql c ...
- java 中的几种 "通用方法“
前言 Java中,除了基本的数值类型,其他所有数据类型(包括数组)都是对象. 而Object这个类是所有类的超类,它提供的方法,自然能够使用于它的所有子类(所有非基本数值类型). 本文介绍了Objec ...
- 项目乱码 GBK转UTF-8工具
项目乱码 GBK转UTF-8工具 链接:http://pan.baidu.com/s/1pLw1mMB 密码:rj6c
- Java BigInteger(大数,ACM比赛专用)
用c或者C++处理大数比较麻烦,于是决心学习一下JAVA中大数运算. 先下载一个eclipse,具体的用法去问度娘吧 JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮 ...
- 正则表达式入门教程&&经典Javascript正则表达式(share)
前言 例子: ^.+@.+\\..+$ 这样的代码曾经多次把我自己给吓退过.可能很多人也是被这样的代码给吓跑的吧.继续阅读本文将让你也可以自由应用这样的代码. 正文 教程:正则表达式30分钟入门教程 ...
- 解决Android时时更新listview数组越界问题
时时更新数据一般出现在金融.股票行业对数据的准确性要求极高情况下使用. 先来看看下面一段代码, public class MainActivity extends Activity { private ...
- 基于时间延迟的Python验证脚本
自己写的一段Python脚本,经常拿来验证一些sqlmap等工具跑不出数据的网站. GET类型: import urllib import urllib2 import time payloads = ...
- JavaWeb学习记录(十九)——jstl自定义标签之简单标签
一.简单标签共定义了5个方法: setJspContext方法 setParent和getParent方法 setJspBody方法 doTag方法 二.方法介绍 osetJspContext方法 用 ...
- 越狱Season 1-Episode 14: The Rat
Season 1, Episode 14: The Rat -Michael: 24 hours from now, 24小时后 my brother is scheduled to die, sch ...