Spring---DI依赖注入

冷不防 2023-07-25 12:47 8阅读 0赞

上一篇介绍了关于IOC的内容:IOC称为控制反转,简单来说就是讲对象的创建的权利以及对象的生命周期的管理过程交给Spring容器来管理,从此在开发的过程中不需要关注对象的创建以及生命周期,而是在需要的时候有spring容器来提供,这个由Spring容器管理对象创建和生命周期的机制称之为控制反转

那么什么是DI,有人说IOC就是DI 我持不同意见,我认为DI是IOC的重要实现方式

  1. **在创建对象的过程中spring容器可依据配置对象的属性进行设置,这个过程称为依赖注入,指在对象在创建的过程中不是自己主动去找,换句话说就是指对象不是从容器中查找它所依赖的类,而是在容器实例化对象的时候就主动将它依赖的类注入给它。**

简单的用一个例子来说明:

新建两个类:Address类 和 Student类

Address类

  1. //Address类
  2. package com.kuang.pojo;
  3. public class Address {
  4. private String address;
  5. public String getAddress(){
  6. return address;
  7. }
  8. public void setAddress(String address){
  9. this.address=address;
  10. }
  11. @Override
  12. public String toString() {
  13. return "Address{" +
  14. "address='" + address + '\'' +
  15. '}';
  16. }
  17. }

Student类

  1. //Student类
  2. package com.kuang.pojo;
  3. import java.util.*;
  4. public class Student {
  5. private String name;
  6. private Address address;
  7. private String[] books;
  8. private List<String> hobbys;
  9. private Map<String,String> card;
  10. private Set<String> games;
  11. private String wife;
  12. private Properties info;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Address getAddress() {
  20. return address;
  21. }
  22. public void setAddress(Address address) {
  23. this.address = address;
  24. }
  25. public String[] getBooks() {
  26. return books;
  27. }
  28. public void setBooks(String[] books) {
  29. this.books = books;
  30. }
  31. public List<String> getHobbys() {
  32. return hobbys;
  33. }
  34. public void setHobbys(List<String> hobbys) {
  35. this.hobbys = hobbys;
  36. }
  37. public Map<String, String> getCard() {
  38. return card;
  39. }
  40. public void setCard(Map<String, String> card) {
  41. this.card = card;
  42. }
  43. public Set<String> getGames() {
  44. return games;
  45. }
  46. public void setGames(Set<String> games) {
  47. this.games = games;
  48. }
  49. public String getWife() {
  50. return wife;
  51. }
  52. public void setWife(String wife) {
  53. this.wife = wife;
  54. }
  55. public Properties getInfo() {
  56. return info;
  57. }
  58. public void setInfo(Properties info) {
  59. this.info = info;
  60. }
  61. @Override
  62. public String toString() {
  63. return "Student{" +
  64. "name='" + name + '\'' +
  65. ", address=" + address +
  66. ", books=" + Arrays.toString(books) +
  67. ", hobbys=" + hobbys +
  68. ", card=" + card +
  69. ", games=" + games +
  70. ", wife='" + wife + '\'' +
  71. ", info=" + info +
  72. '}';
  73. }
  74. }

从这两个类中我们可以很明显的看到Student类是依赖Address类的,因为在Student中引用了Address类型的变量,那么这个时候就需要我们通过某种方法给他们绑定起来

1.Set注入方法:

我们的student中的属性都是具备私有化的,但是他们的set和get方法通常都是暴露在外,此时spring可以通过set()方法将属性的值注入到对象中

重点: 这个时候加入了beans.xml配置文件

  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 http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="address" class="com.kuang.pojo.Address">
  6. <property name="address" value="北京"></property>
  7. </bean>
  8. <bean id="student" class="com.kuang.pojo.Student">
  9. <!-- 第一种普通值注入-->
  10. <property name="name" value="太假"></property>
  11. <!--第二种引用类型注入-->
  12. <property name="address" ref="address"></property>
  13. <!-- 数组注入-->
  14. <property name="books">
  15. <array>
  16. <value>红楼梦</value>
  17. <value>三国</value>
  18. <value>水浒传</value>
  19. <value>西游记</value>
  20. </array>
  21. </property>
  22. <!-- List集合注入-->
  23. <property name="hobbys">
  24. <list>
  25. <value>听歌</value>
  26. <value>跳舞</value>
  27. </list>
  28. </property>
  29. <!-- Map-->
  30. <property name="card">
  31. <map>
  32. <entry key="a" value="1"></entry>
  33. <entry key="b" value="2"></entry>
  34. <entry key="c" value="3"></entry>
  35. </map>
  36. </property>
  37. <!-- Set-->
  38. <property name="games">
  39. <set>
  40. <value>lol</value>
  41. </set>
  42. </property>
  43. <!-- null-->
  44. <property name="wife">
  45. <null></null>
  46. </property>
  47. <!-- properties-->
  48. <property name="info">
  49. <props>
  50. <prop key="driver">123</prop>
  51. <prop key="url"></prop>
  52. </props>
  53. </property>
  54. </bean>
  55. </beans>

看上面的beans.xml配置文件,可能会有点蒙蔽,我们把上面的配置文件分为两部分:

1.1第一部分:属于spring内置的属性(例如:string[]数组 int list map等都属于内置属性)可以直接注入:

  1. <!-- 数组注入-->
  2. <property name="books">
  3. <array>
  4. <value>红楼梦</value>
  5. <value>三国</value>
  6. <value>水浒传</value>
  7. <value>西游记</value>
  8. </array>
  9. </property>
  10. <!-- List集合注入-->
  11. <property name="hobbys">
  12. <list>
  13. <value>听歌</value>
  14. <value>跳舞</value>
  15. </list>
  16. </property>
  17. <!-- Map-->
  18. <property name="card">
  19. <map>
  20. <entry key="a" value="1"></entry>
  21. <entry key="b" value="2"></entry>
  22. <entry key="c" value="3"></entry>
  23. </map>
  24. </property>
  25. <!-- Set-->
  26. <property name="games">
  27. <set>
  28. <value>lol</value>
  29. </set>
  30. </property>
  31. <!-- null-->
  32. <property name="wife">
  33. <null></null>
  34. </property>
  35. <!-- properties-->
  36. <property name="info">
  37. <props>
  38. <prop key="driver">123</prop>
  39. <prop key="url"></prop>
  40. </props>
  41. </property>

1.2第二部分:非spring内置的属性的注入方式:(例如Address类,属于是我们自己创建的)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMwNjMxMDYz_size_16_color_FFFFFF_t_70

测试类:

  1. import com.kuang.pojo.Student;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
  7. Student student=(Student) context.getBean("student");
  8. System.out.println(student.toString());
  9. }
  10. }

结果:

2020041510193938.png

发表评论

表情:
评论列表 (有 0 条评论,8人围观)

还没有评论,来说两句吧...

相关阅读

    相关 依赖注入

    把有依赖关系的类放到容器中,解析出这些类的实例,就是依赖注入。目的是实现类的解耦。 实例:Class A中用到了Class B的对象b,一般情况下,需要在A的代码中显式的ne

    相关 SpringDI和AOP解释

    DI 通过DI,对象的依赖关系将由系统中负责协调各对象的第三方组件在创建对象的时候进行设定。 对象无需自行创建或管理它们的依赖关系,依赖关系将被自动注入到需要它们的对象