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. Google Analytics SEO 实时 网站 访问量 统计

    /*************************************************************************** * Google Analytics SEO ...

  2. dedecms头部标签(标题,关键词,描述标签)(借用)

    先说说dedecms头部标题,关键词,描述标签的作用我相信网络上也有很多这样的信息,那为什么我还要写这个?因为这个对我们初学者来说还是比较重要的,因为做SEO就要用到这些标签.首先我写下首页头部标签我 ...

  3. 华为"128为大整数相加"机试题

    最近正直春招,偶尔接触到了华为的这道大整数相加的测试题,在网上找了一个算法,然后自己尝试进行了优化,最后也对memmove()函数效率有了进一步把握. #include <time.h># ...

  4. 更改SAP GUI 登陆背景

    您也可以定制初始屏幕(SAP Easy Access): 所有用户的全局设置:o   更改位于初始屏幕右侧的登录 -   如果希望将登录放置在初始屏幕右侧,请按如下处理:        使用事务 SW ...

  5. 67. Add Binary

    public class Solution { public String addBinary(String a, String b) { char[] aa=a.toCharArray(); cha ...

  6. 网络延迟查看器 Network latency view 1.4

    这是个用于查看网络延迟/ip/主机/地区的工具,内外网通吃,外网可通过这里下载csv以显示国家(地区) 可以自己决定winpcap或者原始套接字进行捕捉 如果只扫描内网推荐angryip 这是款发布在 ...

  7. 使用Matlab对灰度图像编程实现2D的傅里叶变换

    1.      先载入一幅灰度图像,如下: (非灰度图) 2. 利用函数fft2,对其进行快速傅立叶变换, 并利用函数fftshift 将变换后的图像原点移动到频率矩形的中心. 3. 利用abs()函 ...

  8. js刷新当前页面

    a href="javascript:window.location.href=location.href"

  9. WebApp远程调试工具

    针对存在内嵌在客户端的webview项目中,在开发过程中我们希望能像PC上一下直接进行调试,提高我们平时的开发效率,这时候我们可以使用webApp远程调试工具-weinre. 安装: 本工具需要nod ...

  10. 第一篇TC界面设计

    TC界面设计方法 第一:确定自己最终界面的大小. 第二:根据功能需要,选择相应的控价. 第三:美化界面,对界面的控件调整布局位置,设置字体颜色设置背景图片等等 脚本代码: function 按钮1_点 ...