通过读取xml文件,利用反射动态加载类和方法,其实就是spring的注入机制
模拟,可以清晰的看出整个运行流程 1、配置文件 applicationContext.xml
<beans>
<bean id="sd" class="dao.impl.IStudentDaoImpl"/>
<bean id="studentService" class="service.StudentService">
<property name="studentDao" ref="sd"/> </bean>
</beans>
  property标签中name 为 StudentService.java的属性

2、dao
package dao;
public interface StudentDao {
public void save();
}

 3、daoImpl

 1)

package dao.impl;

import dao.StudentDao;public class IStudentDaoImpl implements StudentDao {
public void save() { System.out.println("IStudentDaoImpl.save()");
}
}

 2)

package dao.impl;

import dao.StudentDao;

public class StudentDaoImpl implements StudentDao{
public void save() {
System.out.println("StudentDaoImpl.save()");
}
}
4、service
package service;

import dao.StudentDao;

public class StudentService {
private StudentDao studentDao; public StudentDao getStudentDao() {
return studentDao;
} public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void save(){
this.studentDao.save();
}
}

5、解析xml文件


package util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder; public class ClassPathXmlApplicationContext {
static Map<String,Object> map=new HashMap<String,Object>();
public void SaxXml() throws Exception{
SAXBuilder sb=new SAXBuilder();
Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("applicationContext.xml"));
Element root=doc.getRootElement();
List<Element> list=root.getChildren("bean");
for(int i=0;i<list.size();i++){
Element ele=list.get(i);
String id=ele.getAttributeValue("id");
String obj=ele.getAttributeValue("class");
Object o=Class.forName(obj).newInstance();
map.put(id, o); for(Element elePro:(List<Element>)ele.getChildren("property")){
String name=elePro.getAttributeValue("name");
String bean=elePro.getAttributeValue("ref");
String methodName="set"+name.substring(0, 1).toUpperCase()+name.substring(1);
System.out.println("MethodName:"+methodName);
Method m=o.getClass().getMethod(methodName, map.get(bean).getClass().getInterfaces()[0]);
m.invoke(o, map.get(bean));
}
}
}
public static Object get(String id){
return map.get(id);
}
}

6、创建测试类

package service;

import static org.junit.Assert.*;

import org.junit.Test;

import util.ClassPathXmlApplicationContext;

public class StudentServiceTest {

    @Test
public void testSave() throws Exception {
ClassPathXmlApplicationContext cpac=new ClassPathXmlApplicationContext();
cpac.SaxXml();
StudentService ss=(StudentService)ClassPathXmlApplicationContext.get("studentService");
ss.save();
} }

输出结果:IStudentDaoImpl.save()  ,因为在xml文件中配置的是IStudentDaoImpl 没有配置StudentDaoImpl,所以反射加载的只是IStudentImpl


  
 

模拟Spring依赖注入的更多相关文章

  1. Spring依赖注入 --- 模拟实现

    Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...

  2. (转)编码剖析Spring依赖注入的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...

  3. Spring依赖注入(IOC)那些事

    小菜使用Spring有几个月了,但是对于它的内部原理,却是一头雾水,这次借着工作中遇到的一个小问题,来总结一下Spring. Spring依赖注入的思想,就是把对象交由Spring容器管理,使用者只需 ...

  4. Spring依赖注入三种方式详解

    在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...

  5. Spring依赖注入:注解注入总结

    更多11   spring   依赖注入   注解   java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...

  6. Spring 依赖注入,在Main方法中取得Spring控制的实例

    Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...

  7. Spring依赖注入 --- 简单使用说明

    Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...

  8. Java Web系列:Spring依赖注入基础

    一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是 ...

  9. Spring依赖注入的三种方式

    看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...

随机推荐

  1. (原)Opencv中直方图均衡和图像动态范围拉伸的代码

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5102032.html 参考网址: http://blog.csdn.net/abcjennifer/a ...

  2. java读取xml(当xml放在包里时)

  3. 轻量级jquery框架之--面板(panel)

    面板需求: (1)支持可拖拽,面板将作为后期的布局组件.window组件.alert组件的基础. (2)支持自定义工具栏,工具栏位置定义在面板底部,工具栏依赖toolbar组件. (3)支持加载JSO ...

  4. 【2】开发环境的搭建,Ubuntu14.04

    这里使用的是Ubuntu14.04 Unity 更新源 首先,将更新源更换为国内更新源,我这里使用的是网易的更新源 sudo gedit /etc/apt/sources.list deb http: ...

  5. djangoPOST请求403 forbidden

    处理过程 网上搜索修改setting.py,在MIDDLEWARE_CLASSES增加django.middleware.csrf.CsrfResponseMiddleware  没能解决问题 有说在 ...

  6. j2ee概览

    J2EE诞生的背景是什么?Java 2平台企业版,也就是J2EE,定义了开发多层企业应用程序的标准.它的诞生并不是偶然的,它是在各种条件积累成熟之下的产物.原因之一:java语言的巨大成功.1994年 ...

  7. 64位Win7安装+32位Oracle + PL/SQL 解决方法

    软件景象:64位win7.32位Oracle 10g. PL/SQL 9.0.4.1644 媒介:以前开辟用的都是32位体系,忽然换到64位上,安装景象真的有点麻烦了,尤其对于PL/SQL只支撑32位 ...

  8. ###Git 基础图解、分支图解、全面教程、常用命令###

    一.Git 基础图解 转自:http://www.cnblogs.com/yaozhongxiao/p/3811130.html Git 图解剖析 git中文件内容并没有真正存储在索引(.git/in ...

  9. No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)

    之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...

  10. 【转】HDMI控制与组态剖析

    HDMI能够称霸为王者之尊吗?目前消费者陷入的困境就是Audio/Video的连接线数量过度庞大,而HDMI最大卖点之一就是可使用单一的连接线完全取代众多的影音连接线,简洁又方便.缺点是技术版本变动太 ...