« 上一页 1 2 3 下一页 »
浏览 9671 次
精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (3)
作者 正文
  • zhouxingfu520
  • 等级: 
  • 性别: 
  • 文章: 108
  • 积分: 550
  • 来自: 广州
 
推荐群组: struts2 
更多相关推荐

用spring也有两年多了 最近一段时间一直在看框架的源代码 从连接池,tomcat到spring 从中学到最多的是代模式理,java反射,设计思想。

我们不但要知其然,还要知其所以然。“知其所以然”的最好 办法就是下载源代码,仔细研读,揣摩并领会源代 码的精义,看看这些经过诸多高手修改的源代码究竟藏有什么玄机,我们能从其中学习到哪些设计思想及设计模式,代码架构如何,软件配置管理又是怎样进行的……,等,我们从源代码中学习的东西太多了。

下面我根据spring源码 简单实现自己的依赖注入  通过xml形式配置   在对象中获取xml文件 获取定义好的bean 从而对bean对应的class 实现实例化   使用接口形式

接口

  1. public interface PersonDao {
  2. public void add();
  3. }

实现类

  1. package cn.leam.dao.impl;
  2. import cn.leam.dao.PersonDao;
  3. public class PersonDaoBean implements PersonDao {
  4. public void add(){
  5. System.out.println("执行add()方法");
  6. }
  7. }

服务接口

  1. public interface PersonService {
  2. public void save();
  3. }

服务实现类

  1. public class PersonServiceBean implements PersonService {
  2. private PersonDao personDao;
  3. public PersonDao getPersonDao() {
  4. return personDao;
  5. }
  6. public void setPersonDao(PersonDao personDao) {
  7. this.personDao = personDao;
  8. }
  9. public void save(){
  10. personDao.add();
  11. }
  12. }

首先配置beans.xml    配置DAO,SERVICE实现类

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="personDao" class="cn.leam.dao.impl.PersonDaoBean"></bean>
  7. <bean id="personService" class="cn.leam.service.impl.PersonServiceBean">
  8. <property name="personDao" ref="personDao"></property>
  9. </bean>
  10. </beans>

下面模拟spring对xml配置的类进行实例化

存放属性的对象

  1. public class prosDefinition {
  2. private String name;
  3. private String ref;
  4. public ProsDefinition(String name, String ref) {
  5. this.name = name;
  6. this.ref = ref;
  7. }
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public String getRef() {
  15. return ref;
  16. }
  17. public void setRef(String ref) {
  18. this.ref = ref;
  19. }
  20. }

存放bean的 对象

  1. public class Definition {
  2. private String id;
  3. private String className;
  4. private List<ProsDefinition> propertys = new ArrayList<ProsDefinition>();
  5. public Definition(String id, String className) {
  6. this.id = id;
  7. this.className = className;
  8. }
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getClassName() {
  16. return className;
  17. }
  18. public void setClassName(String className) {
  19. this.className = className;
  20. }
  21. public List<PropertyDefinition> getPropertys() {
  22. return propertys;
  23. }
  24. public void setPropertys(List<PropertyDefinition> propertys) {
  25. this.propertys = propertys;
  26. }
  27. }

这里是关键点  所有代码都在这里    使用dom4j 解析xml文件中的bean  并获取id和class  再判断元素中是否有引用元素对其一并获取出来存放才Map中 利用java反射一个一个进行实例化

  1. /**
  2. * 学习版容器
  3. *
  4. */
  5. public class LeamClassPathXMLApplicationContext {
  6. private List<Definition> beanDefines = new ArrayList<Definition>();
  7. private Map<String, Object> sigletons = new HashMap<String, Object>();
  8. public LeamClassPathXMLApplicationContext(String filename){
  9. this.readXML(filename);
  10. this.instanceBeans();
  11. this.injectObject();
  12. }
  13. /**
  14. * 为bean对象的属性注入值
  15. */
  16. private void injectObject() {
  17. for(Definition beanDefinition : beanDefines){
  18. Object bean = sigletons.get(beanDefinition.getId());
  19. if(bean!=null){
  20. try {
  21. PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
  22. for(ProsDefinition propertyDefinition : beanDefinition.getPropertys()){
  23. for(PropertyDescriptor properdesc : ps){
  24. if(propertyDefinition.getName().equals(properdesc.getName())){
  25. Method setter = properdesc.getWriteMethod();//获取属性的setter方法
  26. if(setter!=null){
  27. Object value = sigletons.get(propertyDefinition.getRef());
  28. setter.setAccessible(true);
  29. setter.invoke(bean, value);//把引用对象注入到属性
  30. }
  31. break;
  32. }
  33. }
  34. }
  35. } catch (Exception e) {
  36. }
  37. }
  38. }
  39. }
  40. /**
  41. * 完成bean的实例化
  42. */
  43. private void instanceBeans() {
  44. for(Definition beanDefinition : beanDefines){
  45. try {
  46. if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim()))
  47. sigletons.put(beanDefinition.getId(),
  48. Class.forName(beanDefinition.getClassName()).newInstance());
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54. /**
  55. * 读取xml配置文件
  56. * @param filename
  57. */
  58. private void readXML(String filename) {
  59. SAXReader saxReader = new SAXReader();
  60. Document document=null;
  61. try{
  62. URL xmlpath = this.getClass().getClassLoader().getResource(filename);
  63. document = saxReader.read(xmlpath);
  64. Map<String,String> nsMap = new HashMap<String,String>();
  65. nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间
  66. XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
  67. xsub.setNamespaceURIs(nsMap);//设置命名空间
  68. List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点
  69. for(Element element: beans){
  70. String id = element.attributeValue("id");//获取id属性值
  71. String clazz = element.attributeValue("class"); //获取class属性值
  72. Definition beanDefine = new Definition(id, clazz);
  73. XPath propertysub =  element.createXPath("ns:property");
  74. propertysub.setNamespaceURIs(nsMap);//设置命名空间
  75. List<Element> propertys = propertysub.selectNodes(element);
  76. for(Element property : propertys){
  77. String propertyName = property.attributeValue("name");//元素内部引用的属性也获取
  78. String propertyref = property.attributeValue("ref");
  79. ProsDefinition propertyDefinition = new ProsDefinition(propertyName, propertyref);
  80. beanDefine.getPropertys().add(propertyDefinition);
  81. }
  82. beanDefines.add(beanDefine);
  83. }
  84. }catch(Exception e){
  85. e.printStackTrace();
  86. }
  87. }
  88. /**
  89. * 获取bean实例
  90. * @param beanName
  91. * @return
  92. */
  93. public Object getBean(String beanName){
  94. return this.sigletons.get(beanName);
  95. }
  96. }

上面简单的依赖注入 基本完成 当然spring的源码会管家复杂  我们主要是理解其思想  下面我们来测试

  1. public class SpringTest {
  2. @BeforeClass
  3. public static void setUpBeforeClass() throws Exception {
  4. }
  5. @Test public void instanceSpring(){
  6. LeamClassPathXMLApplicationContext ctx = new
  7. LeamClassPathXMLApplicationContext("beans.xml");
  8. PersonService personService = (PersonService)ctx.getBean("personService");
  9. personService.save();
  10. }
  11. }
 

(转载)自己实现spring的更多相关文章

  1. [转载] Thrift-server与spring集成

    转载自http://shift-alt-ctrl.iteye.com/blog/1990026 Thrift服务server端,其实就是一个ServerSocket线程 + 处理器,当Thrift-c ...

  2. [转载] Thrift-client与spring集成

    转载自http://shift-alt-ctrl.iteye.com/blog/1990030?utm_source=tuicool&utm_medium=referral Thrift-cl ...

  3. 转载:在spring中嵌入activemq

    转载:http://www.dev26.com/blog/article/137 web开发站中的邮件发送使用了activemq我这是从网上找的进行了一些修改,记录下来,为了避免发送邮件时程序对用户操 ...

  4. 转载:深入理解Spring MVC 思想

    原文作者:赵磊 原文地址:http://elf8848.iteye.com/blog/875830 目录  一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.sp ...

  5. [转载]快速搭建Spring MVC 4开发环境

    (一)工作环境准备: JDK 1.7 Eclipse Kepler Apache Tomcat 8.0 (二)在Eclipse中新建Maven工程,在Archetype类型中,选择“maven-arc ...

  6. (转载)在spring的bean中注入内部类

    原文链接:http://outofmemory.cn/java/spring/spring-DI-inner-class 在spring中注入内部类,有可能会遇到如下异常信息: 2014-5-14 2 ...

  7. 【转载】加密Spring加载的Properties文件

    目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.rail.comm; import j ...

  8. 用spring tool suite插件创建spring boot项目时报An internal error occurred during: "Building UI model". com/google/common/

    本文为博主原创,未经允许不得转载 在用spring tool suite创建spring boot项目时,报一下异常: 查阅很多资料之后发现是因为装的spring tool suite的版本与ecli ...

  9. spring boot实战(第一篇)第一个案例

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...

  10. @ControllerAdvice 拦截异常并统一处理(转载)

    在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...

随机推荐

  1. Django同步创建models table失败

    django1.8通过manage.py syncdb 执行同步创建models中创建的表格失败 由于syncdb命令在1.9版本中会被remove, 需要改用makemigrations命令进行代替 ...

  2. Linux启用MySQL的InnoDB引擎

    前几天公司的一个项目组的同事反应说公司内部的一台Linux服务器上的MySQL没有InnoDB这个引擎,我当时想应该不可能啊,MySQL默认应该 就已经安装了这个引擎的吧,于是上服务器去看了看,发现还 ...

  3. mysql 导出导入数据库中所有数据

    导出数据库所有数据 >mysqldump -uroot -proot -all hzgajzyz>e:/hzgajzyz.sql 导入数据库所有数据 >source e:/hzgaj ...

  4. Git权威指南 读笔(4)

    第12章 改变历史: $ git commit --amend -m "Remove hello.h, which is useless." 修改提交说明 $ git log -- ...

  5. 【web安全】第六弹:手工SQL注入详解

    前一段时间,在对SQL注入有了新的理解之后,写了这篇文章.本来准备投稿,因为内容过于基础被打回来了,想想屯着也没意思,发出来发出来~~本来有好多图的,但是博客园发图很麻烦,word文档的链接会贴在文章 ...

  6. CODEVS 1090 加分二叉树

    题目描述 Description 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第j个节点的分数为di, ...

  7. JLOI 2013 卡牌游戏

    问题描述: N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先 ...

  8. noj [1482] 嘛~付钱吧!(完全背包)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1482 [1482] 嘛~付钱吧! 时间限制: 1000 ms 内存限制: 65535 K 问题描述 大白菜带着 ...

  9. js构造函数传参

    1.直接传参并用this关键字初始化属性 function Person(name,age,learn){ this.name = name; this.age = age; this.learn = ...

  10. SQL中not and or优先级问题

    SQL中 not  and or优先级问题 刚刚在项目中遇到这样一个问题,SQL语句如下: 我想要的结果的条件是:1. LIBRARY_ID=1 或者 LIB_ID=1 2.STATUS=3 但是结果 ...