[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的核心思想就体现在控制.反转这两个词上面,要理 ...
随机推荐
- 使用ngx_lua构建高并发应用(1)
转自:http://blog.csdn.net/chosen0ne/article/details/7304192 一. 概述 Nginx是一个高性能,支持高并发的,轻量级的web服务器.目前,Apa ...
- Web前端学习笔记(001)
....编号 ........类别 ............条目 ................明细....................时间 一.Web前端学习笔记 ...
- 《JS高程》引用类型学习笔记
2月圆满的结束了,结束之前是如凤凰般的涅槃.一边上班,一边搞科研的忙碌有点让人透不过气,心会不由得浮躁起来.但是,无论什么事情,只要充满耐心.专心去做,总会朝好的方向发展,心态真的很重要.Anyway ...
- sourceMappingURL
JavaScript Source Map 详解
- 右键菜单添加程序,指定图标, Notepad2、Sublime Text 2
右键添加Sublime Text 打开方式 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Sublime Text] ...
- c# ref关键字对于引用类型传递的影响
我们可能见到下面的代码 public static void StringBuilderNoRef(StringBuilder s) { s.Append(" World" ...
- UI学习笔记---第九天UITableView表视图
UITableView表视图 一.表视图的使用场景 表视图UITableView是iOS中最重要的视图,随处可见,通常用来管理一组具有相同数据结构的数据 表视图继承自UIScrollView,所以可以 ...
- ZMMR103-数据批量导入
************************************************************************ Title : ZMMR103 ** Applicat ...
- Harris角点(转载)
1. 不同类型的角点 在现实世界中,角点对应于物体的拐角,道路的十字路口.丁字路口等.从图像分析的角度来定义角点可以有以下两种定义: 角点可以是两个边缘的角点: 角点是邻域内具有两个主方向的特征点: ...
- phpwind8.7升级9.0.1过程(一)本地和服务器数据同步的部署
在使用phpwind的过程中需要将整个网站论坛的模板从phpwind8.7升级到phpwind9.0.1 需要首先在本地搭建和服务器端一样的环境然后在本地尝试性升级之后,然后在服务器端进行升级. 以下 ...