Spring(七):di依赖注入

打印 上一主题 下一主题

主题 858|帖子 858|积分 2574

一、什么是DI依赖注入
依赖关系注入 (DI) 是一个过程,通过该过程,对象仅通过构造函数参数、工厂方法的参数或在构造对象实例或从工厂方法返回后在对象实例上设置的属性来定义其依赖关系(即,使
用它们使用的其他对象)。然后,容器在创建 Bean 时注入这些依赖项。这个过程基本上是Bean本身的反函数(因此得名“控制反转”),通过使用类的直接构造或服务定位器模式来控制
其依赖项的实例化或位置。
使用 DI 原则,代码更清晰,当对象与其依赖项一起提供时,解耦更有效。该对象不查找其依赖项,也不知道依赖项的位置或类。
二、依赖注入的方式
依赖注入有三种方式:基于构造函数的依赖关系注入、利用set方法的依赖关系注入、其他的依赖关系注入。
1.基于构造函数的依赖关系注入
这个在Spring(五)的学习中已经是说过了,这里仅再简单叙述一下构造函数注入的方式。
(1)通过index索引进行注入,索引从0开始。
(2)通过传入参数名进行注入,最方便。
(3)通过传参的类型进行注入,不适用于有同类型的情况。
(4)通过bean进行注入。
2.利用set方法的依赖关系注入
通过set方法进行依赖注入是最核心的注入方式。
这个前面的学习也已经用到了,不过前面注入的都是一些简单的基本类型和String类型,这里我们再对一些复杂的类型如List、Map等进行注入方法的学习。
(1)Student和Address是我们要用到的类
Address
  1. package com.jms.pojo;
  2. public class Address {
  3.     private int id;
  4.     private String address;
  5.     public int getId() {
  6.         return id;
  7.     }
  8.     public void setId(int id) {
  9.         this.id = id;
  10.     }
  11.     public String getAddress() {
  12.         return address;
  13.     }
  14.     public void setAddress(String address) {
  15.         this.address = address;
  16.     }
  17.     @Override
  18.     public String toString() {
  19.         return "Address{" +
  20.                 "id=" + id +
  21.                 ", address='" + address + '\'' +
  22.                 '}';
  23.     }
  24. }
复制代码
Student
  1. package com.jms.pojo;
  2. import java.util.*;
  3. public class Student {
  4.     private String name;
  5.     private Address address;
  6.     private String[] lesson;
  7.     private List<String> hobbys;
  8.     private Map<String, String> card;
  9.     private Set<String> games;
  10.     private String graduate;
  11.     private Properties info;
  12.     public String[] getLesson() {
  13.         return lesson;
  14.     }
  15.     public void setLesson(String[] lesson) {
  16.         this.lesson = lesson;
  17.     }
  18.     public String getName() {
  19.         return name;
  20.     }
  21.     public void setName(String name) {
  22.         this.name = name;
  23.     }
  24.     public Address getAddress() {
  25.         return address;
  26.     }
  27.     public void setAddress(Address address) {
  28.         this.address = address;
  29.     }
  30.     public List<String> getHobbys() {
  31.         return hobbys;
  32.     }
  33.     public void setHobbys(List<String> hobbys) {
  34.         this.hobbys = hobbys;
  35.     }
  36.     public Map<String, String> getCard() {
  37.         return card;
  38.     }
  39.     public void setCard(Map<String, String> card) {
  40.         this.card = card;
  41.     }
  42.     public Set<String> getGames() {
  43.         return games;
  44.     }
  45.     public void setGames(Set<String> games) {
  46.         this.games = games;
  47.     }
  48.     public String getGraduate() {
  49.         return graduate;
  50.     }
  51.     public void setGraduate(String graduate) {
  52.         this.graduate = graduate;
  53.     }
  54.     public Properties getInfo() {
  55.         return info;
  56.     }
  57.     public void setInfo(Properties info) {
  58.         this.info = info;
  59.     }
  60.     @Override
  61.     public String toString() {
  62.         return "Student{" + "\n" +
  63.                 "name='" + name + '\'' + "\n" +
  64.                 "address=" + address.toString() + "\n" +
  65.                 "lesson=" + Arrays.toString(lesson) + "\n" +
  66.                 "hobbys=" + hobbys + "\n" +
  67.                 "card=" + card + "\n" +
  68.                 "games=" + games + "\n" +
  69.                 "graduate='" + graduate + '\'' + "\n" +
  70.                 "info=" + info + "\n" +
  71.                 '}';
  72.     }
  73. }
复制代码
(2)beans.xml
  1. <bean id="student" class="com.jms.pojo.Student">
  2.         
  3.         <property name="name" value="jms"/>
  4.         
  5.         <property name="address" ref="address"/>
  6.         
  7.         <property name="lesson">
  8.             <array>
  9.                 <value>C</value>
  10.                 <value>Java</value>
  11.                 <value>Python</value>
  12.             </array>
  13.         </property>
  14.         
  15.         <property name="hobbys">
  16.             <list>
  17.                 <value>编程</value>
  18.                 <value>写作</value>
  19.                 <value>听音乐</value>
  20.             </list>
  21.         </property>
  22.         
  23.         <property name="card">
  24.             <map>
  25.                 <entry key="学生卡" value="101010101"/>
  26.                 <entry key="饭卡" value="20202020202"/>
  27.                 <entry key="水卡" value="30303030303"/>
  28.             </map>
  29.         </property>
  30.         
  31.         <property name="games">
  32.             <set>
  33.                 <value>game1</value>
  34.                 <value>game2</value>
  35.                 <value>game3</value>
  36.             </set>
  37.         </property>
  38.         
  39.         <property name="graduate">
  40.             <null/>
  41.         </property>
  42.         
  43.         <property name="info">
  44.             <props>
  45.                 <prop key="学号">100001</prop>
  46.                 <prop key="姓名">啊范德萨</prop>
  47.                 <prop key="班级">计算机3班</prop>
  48.             </props>
  49.         </property>
  50.     </bean>
  51.     <bean id="address" class="com.jms.pojo.Address">
  52.         <property name="id" value="1"/>
  53.         <property name="address" value="world"/>
  54.     </bean>
复制代码
(3)测试
  1. @Test
  2.     public void studentTest() {
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
  4.         Student student = applicationContext.getBean("student", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
测试结果:

3.其他的依赖关系注入
 c命名空间注入和p命名空间注入,这是官方给出的拓展的注入方式,p命名空间注入对应set注入,c命名空间注入对应有参构造注入。
使用这两种注入时都需要添加约束,以下是官方给出的使用示例:
p-namespace:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xmlns:p="http://www.springframework.org/schema/p"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.         https://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean name="john-classic" class="com.example.Person">
  7.         <property name="name" value="John Doe"/>
  8.         <property name="spouse" ref="jane"/>
  9.     </bean>
  10.     <bean name="john-modern"
  11.         class="com.example.Person"
  12.         p:name="John Doe"
  13.         p:spouse-ref="jane"/>
  14.     <bean name="jane" class="com.example.Person">
  15.         <property name="name" value="Jane Doe"/>
  16.     </bean>
  17. </beans>
复制代码
c-namespace:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xmlns:p="http://www.springframework.org/schema/p"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.         https://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean name="john-classic" class="com.example.Person">
  7.         <property name="name" value="John Doe"/>
  8.         <property name="spouse" ref="jane"/>
  9.     </bean>
  10.     <bean name="john-modern"
  11.         class="com.example.Person"
  12.         p:name="John Doe"
  13.         p:spouse-ref="jane"/>
  14.     <bean name="jane" class="com.example.Person">
  15.         <property name="name" value="Jane Doe"/>
  16.     </bean>
  17. </beans>        
复制代码
 
 
(本文仅作个人学习记录用,如有纰漏敬请指正)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表