Spring之控制反转IoC_依赖注入DI

本文详细解析Spring IoC(控制反转)与依赖注入的设计理念与实现过程,包括核心接口BeanFactory和ApplicationContext的作用,以及如何通过XML配置文件实现组件间依赖关系的动态注入。同时提供了一个简单的示例,展示如何使用Spring IoC容器管理和创建对象实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring之控制反转IoC_依赖注入DI

 

控制反转(Inversion of Control)IoC

Spring IoC,借助于依赖注入设计模式,使得开发者不用理会对象自身的生命周期及其关系,而且能够改善开发者对J2EE模式的使用。反向控制原理:通常,应用代码需要告知容器或框架,让它们找到自身所需要的类,然后再由应用代码创建待使用的对象实例。因此,应用代码在使用实例之前,需要创建对象实例。然而,IoC模式中,创建对象实例的任务交给IoC容器或框架(实现了IoC设计模式的框架也被称为IoC容器),使得应用代码只需要直接使用实例,这就是IoC。相对IoC 而言,依赖注入(Dependency Injection)的确更加准确的描述了这种设计理念。所谓依赖注入,即组件之间的依赖关系由容器在运行期决定,形象的来说,即由容器动态的将某种依赖关系注入到组件之中。

org.springframework.beans.factory.BeanFactory是Spring IoC容器的实际代表者,IoC容器负责容纳此前所描述的bean,并对bean进行管理。在Spring中,BeanFactory是IoC容器的核心接口。它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。

 

IoC容器的核心接口

核心接口:BeanFactory和AplicationContext

ClassPathXmlApplicationContext, FileSystemXmlApplicationContext

是ApplicationContext的两个实现类,前者从类路径上查找到xml配置文件,后者从文件系统获得这类信息

XmlBeanFactory是BeanFactory的实现类,最常用

AplicationContext继承于BeanFactory

1.
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
2.
String[] configLoations=new String[]{"applicationContext.xml","applicationContext-editor.xml"};
BeanFactory factory=new ClassPathXmlApplicationContext(configLoations);
3.
ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");

 

IoC容器示例

bean

package spring;

import java.util.Map;

public class Person {

    private String name;
    private String age;
    private Map<String, Object> map;

    public Person() {
    }

    public Person(String name, String age) {
        this.name = name;
        this.age = age;
    }


    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
}

配置文件信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd">

    <!--简单的配置一个bean-->
    <bean id="person0" class="spring.Person">
        <property name="name">
            <value>lyx</value>
        </property>
        <property name="age">
            <value>20</value>
        </property>
    </bean>

    <!--第一个person-->
    <bean id="person" class="spring.Person">
        <constructor-arg name="name" value="lyx"/>
        <constructor-arg name="age" value="12"/>
        <property name="map">
            <map>
                <entry key="a" value="a"/>
                <entry key="b" value="b"/>
                <entry key="c" value="c"/>
            </map>
        </property>
    </bean>

    <!--第二个person-->
    <bean id="person2" class="spring.Person" p:name="lyx" p:age="12" p:map-ref="map"/>
    <util:map id="map" map-class="java.util.HashMap">
        <entry key="a" value="a"/>
        <entry key="b" value="b"/>
        <entry key="c" value="c"/>
    </util:map>
</beans>

测试:

@Test
public void test7668() {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
    Person person = (Person) context.getBean("person");
    System.out.println(person.getName());
    System.out.println(person.getMap().get("a"));
}

 

简单模拟IoC容器的实现

简单的xml配置文件信息

<!--简单的配置一个bean-->
<bean id="person0" class="spring.Person">
    <property name="name">
        <value>lyx</value>
    </property>
    <property name="age">
        <value>20</value>
    </property>
</bean>

 

BeanFactory.java

package spring;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * bean工厂类.
 */
public class BeanFactory {
    private Map<String, Object> beanMap = new HashMap<String, Object>();

    /**
     * bean工厂的初始化.
     *
     * @param xml xml配置文件
     */
    public void init(String xml) {
        try {
            //读取指定的配置文件
            SAXReader reader = new SAXReader();
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            //从class目录下获取指定的xml文件
            InputStream ins = classLoader.getResourceAsStream(xml);
            Document doc = reader.read(ins);
            Element root = doc.getRootElement();
            Element foo;

            //遍历bean
            for (Iterator i = root.elementIterator("bean"); i.hasNext(); ) {
                foo = (Element) i.next();
                //获取bean的属性id和class
                Attribute id = foo.attribute("id");
                Attribute cls = foo.attribute("class");

                //利用Java反射机制,通过class的名称获取Class对象
                Class bean = Class.forName(cls.getText());

                //获取对应class的信息
                BeanInfo info = Introspector.getBeanInfo(bean);
                //获取其属性描述
                PropertyDescriptor pd[] = info.getPropertyDescriptors();
                //设置值的方法
                Method mSet = null;
                //创建一个对象
                Object obj = bean.newInstance();

                //遍历该bean的property属性
                for (Iterator ite = foo.elementIterator("property"); ite.hasNext(); ) {
                    Element foo2 = (Element) ite.next();
                    //获取该property的name属性
                    Attribute name = foo2.attribute("name");
                    String value = null;

                    //获取该property的子元素value的值
                    for (Iterator ite1 = foo2.elementIterator("value"); ite1.hasNext(); ) {
                        Element node = (Element) ite1.next();
                        value = node.getText();
                        break;
                    }

                    for (int k = 0; k < pd.length; k++) {
                        if (pd[k].getName().equalsIgnoreCase(name.getText())) {
                            mSet = pd[k].getWriteMethod();
                            //利用Java的反射机制调用对象的某个set方法,并将值设置进去
                            mSet.invoke(obj, value);
                        }
                    }
                }

                //将对象放入beanMap中,其中key为id值,value为对象
                beanMap.put(id.getText(), obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过bean的id获取bean的对象.
     *
     * @param beanName bean的id
     * @return 返回对应对象
     */
    public Object getBean(String beanName) {
        Object obj = beanMap.get(beanName);
        return obj;
    }
}

测试:

@Test
public void test1213() {
    BeanFactory factory = new BeanFactory();
    factory.init("spring-config.xml");
    Person person = (Person) factory.getBean("person0");
    System.out.println(person.getName());
    System.out.println(person.getAge());
}

总之,就是通过反射和内省机制来实例化一个对象,并且set属性值。

====END====

转载于:https://my.oschina.net/xinxingegeya/blog/297360

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值