(转载)自己实现spring

时间:2025-01-14 11:07:02
« 上一页 1 2 3 下一页 »
浏览 9671 次
精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (3)
作者 正文
  • zhouxingfu520
  • 等级: (转载)自己实现spring
  • (转载)自己实现spring
  • 性别: (转载)自己实现spring
  • 文章: 108
  • 积分: 550
  • 来自: 广州
  • (转载)自己实现spring
推荐群组: 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