模拟Spring依赖注入
通过读取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依赖注入的更多相关文章
- Spring依赖注入 --- 模拟实现
Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...
- (转)编码剖析Spring依赖注入的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...
- Spring依赖注入(IOC)那些事
小菜使用Spring有几个月了,但是对于它的内部原理,却是一头雾水,这次借着工作中遇到的一个小问题,来总结一下Spring. Spring依赖注入的思想,就是把对象交由Spring容器管理,使用者只需 ...
- Spring依赖注入三种方式详解
在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...
- Spring依赖注入:注解注入总结
更多11 spring 依赖注入 注解 java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...
- Spring 依赖注入,在Main方法中取得Spring控制的实例
Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...
- Spring依赖注入 --- 简单使用说明
Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...
- Java Web系列:Spring依赖注入基础
一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是 ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
随机推荐
- (转) 制作 Clonezilla live 启动盘
GNU/Linux Method A: Tuxboot 下載 GNU/Linux 版本使用的 Tuxboot 在您的環境 在 GNU/Linux 下, 請依 指示 來執行 Tuxboot 並安裝再生龍 ...
- android ant 最简单的打包签名,混淆方法
使用ant打包,如果脚本都是我们自己一步一步来写的话,是一个比较麻烦的东西. 关于ant,我们详细看下: ant支持 ant debug,ant release等命令,我们需要签名混淆,那么就需要an ...
- java 数字补齐0
String str_f = str.substring(0, 1); int i = (Integer.parseInt(str.substring(1)) + 1); // 数字补齐0 Decim ...
- [Head First Python]5. summary
1- "原地"排序-转换后替换 >>> list = [2,1,3] >>> list.sort() >>> list [1, ...
- https://github.com/aptana/studio3/releases aptana
https://github.com/aptana/studio3/releases aptana
- robot自动化测试(二)--- template使用
首先贴一下测试结构 测试流程: 1.打开百度主页 2.搜索框输入搜索内容 3.获取浏览器title 4.验证title的正确性 百度搜索业务关键字:采用分层结构:只需输入要搜索的内容:${search ...
- LeetCode_Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Qt之HTTP上传/下载(继承QNetworkAccessManager,包括使用了authenticationRequired认证信号)
效果 QNetworkAccessManager DownloadNetworkManager::DownloadNetworkManager(QObject *parent) : QNetworkA ...
- Linux系统编程(22)——响应信号
进程对信号的响应 进程可以通过三种方式来响应一个信号: 1.忽略信号,即对信号不做任何处理,其中,有两个信号不能忽略:SIGKILL及SIGSTOP: 2.捕捉信号.定义信号处理函数,当信号发生时,执 ...
- Jump Game II 解答
Question Given an array of non-negative integers, you are initially positioned at the first index of ...