`
freewxy
  • 浏览: 342682 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

编码剖析Spring依赖注入的原理

 
阅读更多

 

一、注入依赖对象

基本类型对象注入:

<bean id=”orderService” class=”com.wxy.service.OrderServiceBean”>

   <constructor-arg index=”0” type=”java.lang.String” value=”xxx”/>//构造器注入

   <property name=”name” value=”wxy”/>//属性setter方法注入

</bean>



注入其他bean:

 方式一:

<bean id=”orderDao” class=”com.wxy.service.OrderDaoBean”/>

<bean id=”orderService” class=”com.wxy.service.OrderServiceBean”>

<property name=”orderDao” ref=”orderDao”/>

 </bean>

 

 

方法二:(使用内部bean,但该bean不能被其他bean使用)

<bean id=”orderService” class=”com.wxy.service.OrderServiceBean”>

      <property name=”orderDao”>

           <bean class=”com.wxy.service.OrderDaoBean”/>

      </property>

</bean>

 

 

 

二、依赖注入(Dependency Injection

所谓依赖注入就是指:在运行期,由外部容器动态的将依赖对象注入到组件中。

 

 

三、编码实现依赖注入功能:

1、 修改beans.xml,添加依赖配置:

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

         <bean id="peopleDao" class="com.wxy.dao.impl.PeopleDaoBean"></bean>

         <bean id="peopleService" class="com.wxy.service.impl.PeopleServiceBean">

         <property name="peopleDap" ref="peopleDao"></property>

         <property name="xx" ref="xxDao"></property>

         </bean>

</beans>

 1.1

 

 

创建dao类:

package com.wxy.dao.impl;

import com.wxy.dao.PeopleDao;

/**

*   Dao类,实现数据的持久化操作 

*   @create-time     2011-8-10   下午07:25:26   

*   @revision          $Id

*/

public class PeopleDaoBean implements PeopleDao {


    /* (non-Javadoc)

     * @see com.wxy.dao.impl.PeopleDao#add()

     */

    public void add() {

        System.out.println("this is the method PeopleDaoBean.add()!");

    }

}

 

 

 

 

   1.2创建dao接口:

package com.wxy.dao;

public interface PeopleDao {

    public abstract void add();

}

 

 

 

 

2、新建PropertyDefinition类,存放beanproperty属性:

package com.wxy.bean;

 

/**

*   存放bean的property属性 

*   @create-time     2011-8-10   下午04:40:07   

*   @revision          $Id

*/

public class PropertyDefinition {

    private String name; //属性名
   private String ref;  //属性依赖对象


    /**

     * @return the name

     */

    public String getName() {

        return name;

    }

    /**

     * @param name the name to set

     */

    public void setName(String name) {

        this.name = name;

    } 

    /**

     * @return the ref

     */

    public String getRef() {

        return ref;

    }

    /**

     * @param ref the ref to set

     */

    public void setRef(String ref) {

        this.ref = ref;

    }

    public PropertyDefinition(String name, String ref) {

        super();

        this.name = name;

        this.ref = ref;

    }

}

 

 

 

2.1、 BeanDefinition中加入PropertyList属性,存放property列表

package com.wxy.bean;

 

import java.util.ArrayList;

import java.util.List;

 

public class BeanDefinition {

    private String                   id;

    private String                   className;

    private List<PropertyDefinition> properties = new       ArrayList<PropertyDefinition>(); //存放bean的属性列表 

    public BeanDefinition(String id, String className) {

        this.id = id;

        this.className = className;

    }


    /**

     * @return the id

     */

    public String getId() {

        return id;

    }


    /**

     * @param id the id to set

     */

    public void setId(String id) {

        this.id = id;

    }

    /**

     * @return the className

     */

    public String getClassName() {

        return className;

    }

    /**

     * @param className the className to set

     */

    public void setClassName(String className) {

        this.className = className;

}

/**

     * @return the properties

     */

    public List<PropertyDefinition> getProperties() {

        return properties;

    }

    /**

     * @param properties the properties to set

     */

    public void setProperties(List<PropertyDefinition> properties) {

        this.properties = properties;

    }
}

 

 

 

 

  2.2配置peopleServiceBean,实现注入功能:

package com.wxy.service.impl;

 

import com.wxy.dao.PeopleDao;

import com.wxy.service.PeopleService;

 

/**

*   PeopleServiceBean

*   @create-time     2011-8-9   下午11:07:03   

*   @revision          $Id:PeopleServiceBean.java

*/

public class PeopleServiceBean implements PeopleService {

    private PeopleDao peopleDao;

 
    /**

     * @return the peopleDao

     */

    public PeopleDao getPeopleDao() {

        return peopleDao;

    }

    public void save() {

        System.out.println("--> the method is called save()!");

        peopleDao.add();

    }

    /**

     * @param peopleDao the peopleDao to set

     */

    public void setPeopleDao(PeopleDao peopleDao) {

        this.peopleDao = peopleDao;

    }
}

 

 

 

 

3、进入WxyClassPathXMLApplicationContext中,自己编码实现依赖注入内部功能:

 package com.wxy.content;

 

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.net.URL;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.XPath;

import org.dom4j.io.SAXReader;

 

import com.wxy.bean.BeanDefinition;

import com.wxy.bean.PropertyDefinition;

 

/**

*  自定义IoC容器 

*  BeanDefinition的resource定位:readXML();

*  BeanDefinition的载入和解析 :readXML();

*  BeanDefinition在IoC容器中的注册 instanceBeans();

*   @create-time     2011-8-10   上午09:19:17   

*   @revision          $Id

*/

public class WxyClassPathXMLApplicationContext {

 

    //存放BeanDefinition的列表,在beans.xml中定义的bean可能不止一个

    private final List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();

    //将类名作为索引,将创建的Bean对象存入到Map中

    private final Map<String, Object>  sigletons   = new HashMap<String, Object>();

 

    public WxyClassPathXMLApplicationContext(String fileName) {

        //读取xml配置文件

        this.readXML(fileName);

        //实例化bean

        this.instanceBeans();

        //注入对象

        this.injectObject();

    }

 

    /**

     * 注入对象方法

     */

    private void injectObject() {

        for (BeanDefinition beanDefinition : beanDefines) {

            //获取beanDefines中的对象

            Object bean = sigletons.get(beanDefinition.getId());

            if (bean != null) {

                //如果存在,利用反射技术将值注入

                try {

                    PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass())

                        .getPropertyDescriptors();

                    for (PropertyDefinition propertyDefinition : beanDefinition.getProperties()) {

                        for (PropertyDescriptor properdesc : ps) {

                            //取得属性名字与propertyDefinition中的属性做比较

                            if (propertyDefinition.getName().equals(properdesc.getName())) {

                                //获得属性的setter方法

                                Method setter = properdesc.getWriteMethod();

                                if (setter != null) {

                                    Object value = sigletons.get(propertyDefinition.getRef());

                                    //把引用对象注入到属性

                                    setter.setAccessible(true);//允许访私有方法

                                    setter.invoke(bean, value);

                                }

                            }

                        }

                    }

                } catch (SecurityException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                } catch (IllegalArgumentException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                } catch (IntrospectionException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                } catch (IllegalAccessException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                } catch (InvocationTargetException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }

        }

    }

 

    /**

     * 读取XML配置文件,获取BeanDefinition内容,存入到beanDefinition列表中

     * @param fileName xml配置文件名称

     */

 

    private void readXML(String fileName) {

        SAXReader saxReader = new SAXReader();

        Document document = null;

        try {

            //通过类加载器获取Resource资源路径,实现BeanDefinition的resource定位

            URL xmlPath = this.getClass().getClassLoader().getResource(fileName);

            //将xml读入到document中

            document = saxReader.read(xmlPath);

            Map<String, String> nsMap = new HashMap<String, String>();

            //加入命名空间

            nsMap.put("ns", "http://www.springframework.org/schema/beans");

            //创建beans/bean查询路径,注意:路径前要注明命名空间,便于解析

            XPath xsub = document.createXPath("//ns:beans/ns:bean");

            //设置命名空间

            xsub.setNamespaceURIs(nsMap);

            //获取文档下的所有Bean节点

            List<Element> beans = xsub.selectNodes(document);

            for (Element element : beans) {

                //获取id属性值

                String id = element.attributeValue("id");

                //获取class属性值

                String clazz = element.attributeValue("class");

                BeanDefinition beanDefinition = new BeanDefinition(id, clazz);

                //创建bean/property查询路径

                XPath propertysub = element.createXPath("ns:property");

                //设置命名空间

                propertysub.setNamespaceURIs(nsMap);

                //获取bean的property列表节点

                List<Element> properties = propertysub.selectNodes(element);

                for (Element property : properties) {

                    String propertyName = property.attributeValue("name");

                    String propertyref = property.attributeValue("ref");

                    PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName,

                        propertyref);

                    System.out.println(property);

                    //将property属性添加到beanDefinition中

                    beanDefinition.getProperties().add(propertyDefinition);

                }

                //将新创建的BeanDefinition赌侠ing放入到BeanDeifnitions中

                beanDefines.add(beanDefinition);

            }

        } catch (Exception e) {

            System.out.println(e.toString());

        }

    }

 

    /**

     * 实例化bean,存入到sigletons中

     */

    private void instanceBeans() {

        for (BeanDefinition beanDefinition : beanDefines) {

            try {

                if (beanDefinition.getClassName() != null

                    && !(beanDefinition.getClassName().isEmpty())) {

                    //利用java反射机制,生成BeanDefinition实例,并将其注册到sigletons中

                    sigletons.put(beanDefinition.getId(), Class.forName(

                        beanDefinition.getClassName()).newInstance());

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

 

    }

 

    /**

     * 根据ID名获取实例bean

     * return 返回一个Object对象,用户使用时,需要对获取的结果进行转换类型

     */

    public Object getBean(String beanName) {

        return this.sigletons.get(beanName);

    }

} 

 

 

 

  测试一下看是否取到了property的属性值:

public class Test {

 

    public static void main(String[] args) {

        //IOC容器实例化

        WxyClassPathXMLApplicationContext ac = new WxyClassPathXMLApplicationContext("beans.xml");

        PeopleService peopleService = (PeopleService) ac.getBean("peopleService");

        peopleService.save();

    }

}

 测试结果:

---------------------------------------

--> the method is called save()!
this is the method PeopleDaoBean.add()!

----------------------------------------

 

 

 

分享到:
评论

相关推荐

    Spring学习笔记(6)----编码剖析Spring依赖注入的原理

    本篇学习笔记将深入剖析Spring依赖注入的原理,通过源码分析帮助我们理解这一核心机制。 首先,依赖注入允许我们解耦组件之间的关系,使得各个组件可以独立地进行开发、测试和维护。在Spring中,DI主要通过两种方式...

    Spring2.5视频教程_编码剖析Spring管理Bean的原理

    《Spring2.5视频教程:编码剖析Spring管理Bean的原理》是由知名教育机构黑马程序员的讲师黎活明主讲的一套教程,旨在深入解析Spring框架2.5版本中Bean的管理机制。本教程通过实际编码演示,帮助学习者理解Spring如何...

    编码剖析Spring装配基本属性的原理

    4. Spring的依赖注入(DI): DI是Spring的核心特性,它允许我们在不直接创建对象的情况下控制对象之间的依赖关系。Spring容器负责创建对象、管理和装配它们的依赖。通过XML或注解,我们可以声明依赖关系,Spring会...

    深入解析SPRING架构与设计原理(第2版) 带标签 高清版,深入剖析spring运行机制

    1. **依赖注入**:Spring的核心特性之一,通过DI,对象之间的依赖关系不再硬编码,而是由容器在运行时动态管理和维护。这提高了代码的可测试性和可维护性。 2. **AOP**:Spring的AOP模块允许开发者定义“切面”,...

    spring原理技术详细解析 深入剖析

    1. **依赖注入(Dependency Injection, DI)**:Spring的核心特性之一,它通过反转控制权,使对象之间的依赖关系不再硬编码,而是通过配置或注解来管理。DI降低了组件之间的耦合度,提高了代码的可测试性和可维护性...

    编码剖析@Resource注解的实现原理

    在Spring框架中,`@Resource`注解是一个重要的依赖注入(DI)工具,它用于标记字段、方法或方法参数,以便Spring容器能够自动装配所需的bean。这篇文章将深入探讨`@Resource`注解的工作原理,以及它如何与Spring的IoC...

    Spring技术内幕:深入解析Spring架构与设计原理

    《Spring技术内幕:深入解析Spring架构与设计原理》是一本深度剖析Spring框架核心概念和技术的专著。本书旨在帮助读者理解Spring的内在运作机制,掌握其设计思想,并能灵活运用到实际开发中。通过深入学习,我们可以...

    SPRING技术内幕:深入解析SPRING架构与设计原理第2版

    2. **依赖注入**:Spring的核心特性之一,它允许对象之间的关系在运行时动态地配置,而不是硬编码在类内部。这使得组件之间的耦合度降低,提高了代码的可测试性和可维护性。 3. **IoC容器**:Spring的IoC容器负责...

    84丨开源实战四(上):剖析Spring框架中蕴含的经典设计思想或原则1

    低侵入性意味着Spring框架并不会强制改变或限制应用的结构,而是通过依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)来管理对象间的依赖关系,降低了组件间的耦合度。...

    Spring 技术内幕高清版本

    《Spring技术内幕》是一本深度剖析Spring框架的专业书籍,它为Java程序员、Spring开发者、平台开发人员、系统架构师以及热衷于开源软件源代码研究的程序员提供了宝贵的资源。本书详细讲解了Spring的核心机制和设计...

    精通spring2.0源代码

    1. **依赖注入(Dependency Injection,DI)**:Spring的核心功能之一,通过DI,对象之间的依赖关系不再硬编码在类内部,而是通过配置文件或注解来管理。这样可以降低类之间的耦合度,提高代码的可测试性和可维护性...

    spring源码深入解析

    1. **依赖注入(Dependency Injection,DI)**:Spring的核心特性之一是依赖注入,它允许对象之间的关系在运行时被外部容器管理,而非在代码中硬编码。这使得应用程序更加灵活,易于测试和维护。在Spring5中,这一...

    spring in Action

    《Spring in Action》是一本深度剖析Spring框架的权威指南,专为那些希望深入理解并熟练应用Spring技术的Java开发者设计。这本书涵盖了Spring的核心概念、架构以及实战应用,旨在帮助读者全面掌握Spring框架,并能将...

    spring-framework-4.1.6.RELEASE

    《深入剖析Spring Framework 4.1.6.RELEASE》 Spring Framework是Java开发中的一个核心框架,它为构建可维护、高性能和灵活的Java应用程序提供了基础。4.1.6.RELEASE版本是Spring的重要里程碑,引入了多项改进和...

    spring-analysis,弹簧座.zip

    《深入剖析Spring框架:开源项目spring-analysis》 Spring框架,作为Java开发领域中的核心组件,以其灵活、可扩展性及全面的功能集深受开发者喜爱。本文将深入探讨开源项目"spring-analysis",它为我们提供了理解...

    Spring in action 加随书源代码

    《Spring in Action》是一本深度剖析Spring框架的权威著作,其随书源代码包含了书中所有示例项目的实现,为读者提供了实践和学习Spring框架的宝贵资源。这本书详细讲解了Spring框架的各种核心特性和应用场景,包括...

    spring-framework-4.2.x.zip

    《深入剖析Spring Framework 4.2.x》 Spring Framework作为Java开发中的核心框架,自诞生以来就以其强大的功能和灵活性赢得了广大开发者喜爱。4.2.x版本是Spring发展的一个重要阶段,它带来了许多新特性、优化和...

    晾晾多年珍藏spring开发指南.pdf

    《Spring开发指南》是一本深度剖析Spring框架的珍贵资料,旨在帮助开发者全面理解并熟练运用Spring进行企业级应用开发。Spring作为Java领域的主流框架,以其强大的功能和灵活的设计深受开发者喜爱。该指南深入浅出地...

    Spring5 源码分析2020.zip

    Spring作为Java企业级应用中最广泛应用的框架之一,其强大之处在于它的依赖注入(Dependency Injection, DI)和面向切面编程(Aspect-Oriented Programming, AOP)等核心特性。通过深入学习Spring的源码,我们可以...

Global site tag (gtag.js) - Google Analytics