通过读取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. 13年山东省赛 Mountain Subsequences(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec   ...

  2. 迁移google code上的项目到本地版本库

    今年五月份以来就已经连接不上google code了,翻*墙又极度不稳定,在忍受了几个月之后终于决定将项目搬离google code;经过研究之后终于实现了搬迁到本地,最后总结成下文.一者期望对有需要 ...

  3. Nubiers to follow

    VGG Andrea Vedaldi Berkeley Trevor Darrell Jeff Donahue Ross Girshick Sergio Guadarrama Stanford And ...

  4. KeyTool

    http://ln-ydc.iteye.com/blog/1335213 http://lukejin.iteye.com/blog/605634

  5. QTableView中嵌入复选框CheckBox 的四种方法总结

    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...

  6. SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息

    原文:SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2 ...

  7. css案例学习之双斜角横线菜单

    效果 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  8. C#透过PerformanceCounter取得特定Process的CPU使用率

  9. Linux系统编程(21)——信号的产生

    1.通过终端按键产生信号 通过上一篇我们知道了SIGINT的默认处理动作是终止进程,SIGQUIT的默认处理动作是终止进程并且Core Dump,现在我们来验证一下. 首先解释什么是Core Dump ...

  10. 【转】Compile、Make和Build的区别

    原文网址:http://lavasoft.blog.51cto.com/62575/436216 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任 ...