轻松理解依赖注入

首先,个人觉得理解依赖注入要弄清楚三个问题:1、什么是依赖注入;2、它有何作用;3、怎么使用依赖注入。
那么什么是依赖注入呢,依赖注入的前提是面向接口编程,没有面向接口编程就没有依赖注入,依赖注入是实现控制反转的最常用的方法。那么控制反转又是用来干嘛的呢?控制反转是用来解耦的,所以依赖注入要处理的就是解耦。

1.依赖注入的概念
指程序在运行过程中,如果需要另外一个对象协助完成时,无需在代码中创建被调用者,而是依赖外部的注入获取。
从概念上来理解:用代码描述传统的依赖注入。

     B b = new B();
     A a = new A();
     b.setA(a);

2.依赖注入的三种方式
◆设置注入:设置注入是通过setter方法注入被调用者的实例。
◆构造注入:利用构造方法来设置依赖注入的方式称为构造注入。
◆接口注入:利用接口来设置依赖注入的方式称为接口注入。

3.Talk is cheap. Show the code
3.1设置注入代码示例

package wu.bean;

public class A {
    private String name;

    public String getName() {
        return name;
    }

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

}
package wu.bean;

public class B {
    private A a;// 依赖

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }

}
<?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:mvc="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 将A配置到配置文件中 -->
    <bean name="A" class="wu.bean.A">
        <!-- 将A的属性配置,Spring会自动将配置的值注入到A中 -->
        <property name="name" value="tom"></property>
    </bean>

    <bean name="B" class="wu.bean.B">
        <!-- ref表示要将Bean A注入 到B中 属性是a属性-->
        <property name="a" ref="A"></property>
    </bean>
</beans>

测试代码:

package wu.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import wu.bean.A;
import wu.bean.B;

public class Test {

    public static void main(String[] args) {
        // 1 创建容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        // ClassPathXmlApplicationContext ac = new
        // ClassPathXmlApplicationContext("applicationContext.xml");

        // 2 获取Bean
        /*
         * A a = (A) ac.getBean("A"); System.out.println(a.getName());
         */

        B b = (B) ac.getBean("B");
        System.out.println(b.getA().getName());

    }

}

3.2构造注入代码示例

package DI1;

    public class User {  
        private String name ;  
        private int age ;  
        private String country ;  

        public User(String name, int age, String country) {  
            this.name = name;  
            this.age = age;  
            this.country = country;  
        }  

        public String toString(){  
            return name + " is " + age + " years old,living in " + country ;   
        }  
    }  
<?xml version="1.0" encoding="UTF-8"?>  
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans.xsd"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns="http://www.springframework.org/schema/beans">   
    <bean id="user" class="DI1.User">  
        <constructor-arg value="Jun Wu" />  
        <constructor-arg value="18" />  
        <constructor-arg value="China" />  
    </bean>  
</beans>  

测试代码:

package DI1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {  
    public static void main(String args[]){  
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml") ;  
        User user = (User)context.getBean("user") ;  
        System.out.println(user) ;  
    }  
}  

3.3接口注入代码示例
接口:

package wu;

public interface ProD {
    void addP();

}
package wu;

public class ProDImp implements ProD {

    @Override
    public void addP() {
        System.out.println("add P");

    }

}
package wu;

public class PM {
    private ProDImp proD;

    public ProDImp getProD() {
        return proD;
    }

    public void setProD(ProDImp proD) {
        this.proD = proD;
    }

    public void addU() {
        this.proD.addP();
    }

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

    <bean id="prodimp" class="wu.ProDImp">  
    </bean>  
    <bean id="pm" class="wu.PM">  
        <property name="proD" ref="prodimp"></property>
    </bean>  
</beans>  

测试代码:

package wu;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    private static BeanFactory factory;

    public static void main(String[] args) {
        factory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        PM pm = (PM) factory.getBean("pm");
        pm.addU();
    }

}

以上代码均是完整代码,自己测试成功!请多多指教!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值