Spring 依赖注入

6. 依赖注入

6.1 构造器注入

前面已经说过

6.2 set方式注入※

  • 依赖注入:本质是set注入!

  • 依赖:bean对象的创建依赖于容器

  • 注入:bean对象中的所有属性,由容器来注入

  • 环境搭建

    1. 复杂类型

      package com.peng.pojo;
      
      public class Address {
          private String address;
      
          public String getAddress() {
              return address;
          }
      
          public void setAddress(String address) {
              this.address = address;
          }
          
          @Override
          public String toString() {
              return "Address{" +
                      "address='" + address + '\'' +
                      '}';
          }
      }
      
    2. 真实测试对象

      package com.peng.pojo;
      
      import java.util.*;
      
      public class Student {
          private String name;
          private Address address;
          private String[] book;
          private List<String> hobbys;
          private Map<String,String> card;
          private Set<String> games;
          private String wife;
          private Properties info;
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public Address getAddress() {
              return address;
          }
      
          public void setAddress(Address address) {
              this.address = address;
          }
      
          public String[] getBook() {
              return book;
          }
      
          public void setBook(String[] book) {
              this.book = book;
          }
      
          public List<String> getHobbys() {
              return hobbys;
          }
      
          public void setHobbys(List<String> hobbys) {
              this.hobbys = hobbys;
          }
      
          public Map<String, String> getCard() {
              return card;
          }
      
          public void setCard(Map<String, String> card) {
              this.card = card;
          }
      
          public Set<String> getGames() {
              return games;
          }
      
          public void setGames(Set<String> games) {
              this.games = games;
          }
      
          public String getWife() {
              return wife;
          }
      
          public void setWife(String wife) {
              this.wife = wife;
          }
      
          public Properties getInfo() {
              return info;
          }
      
          public void setInfo(Properties info) {
              this.info = info;
          }
      
          @Override
          public String toString() {
              return "Student{" +
                      "name='" + name + '\'' +
                      ", address=" + address.toString() +
                      ", book=" + Arrays.toString(book) +
                      ", hobbys=" + hobbys +
                      ", card=" + card +
                      ", games=" + games +
                      ", wife='" + wife + '\'' +
                      ", info=" + info +
                      '}';
          }
      }
      
    3. 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
              https://www.springframework.org/schema/beans/spring-beans.xsd">
      
          <bean id="address" class="com.peng.pojo.Address">
              <property name="address" value="葫芦岛"/>
          </bean>
          <bean id="student" class="com.peng.pojo.Student">
              <!--1.普通值注入,value-->
              <property name="name" value="peng"/>
      
              <!--2.其他高级类型注入,bean注入,ref-->
              <property name="address" ref="address"/>
      
              <!--3.数组注入,ref-->
              <property name="book">
                  <array>
                      <value>红楼梦</value>
                      <value>西游记</value>
                      <value>水浒传</value>
                      <value>三国演义</value>
                  </array>
              </property>
      
              <!--4.List-->
              <property name="hobbys">
                  <list>
                      <value>吃饭</value>
                      <value>睡觉</value>
                  </list>
              </property>
      
              <!--5.Map-->
              <property name="card">
                  <map>
                      <entry key="身份证" value="132151313213513553"/>
                      <entry key="银行卡" value="165562612020201234"/>
                  </map>
              </property>
      
              <!--6.Set-->
              <property name="games">
                  <set>
                      <value>LOL</value>
                      <value>CF</value>
                  </set>
              </property>
      
              <!--7.null,空值注入-->
              <property name="wife">
                  <null/>
              </property>
              
              <!--8.Properties-->
              <property name="info">
                  <props>
                      <prop key="学号">20200525</prop>
                      <prop key="性别">男</prop>
                      <prop key="生日">20010604</prop>
                  </props>
              </property>
          </bean>
      </beans>
      
    4. 测试类

      import com.peng.pojo.Student;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class MyTest {
          public static void main(String[] args) {
              ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
              Student student = (Student) context.getBean("student");
              System.out.println(student.toString());
          }
      }
      
    5. 输出

      Student{name='peng', address=Address{address='葫芦岛'}, book=[红楼梦, 西游记, 水浒传, 三国演义], hobbys=[吃饭, 睡觉], card={身份证=132151313213513553, 银行卡=165562612020201234}, games=[LOL, CF], wife='null', info={学号=20200525, 生日=20010604, 性别=男}}
      

6.3 其他方式

  • p标签:xmlns:p="http://www.springframework.org/schema/p"
  • c标签:xmlns:c="http://www.springframework.org/schema/c"

User.java

package com.peng.pojo;

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

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

userbeans.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以直接注入属性的值,property-->
    <bean id="user" class="com.peng.pojo.User" p:name="peng" p:age="18"/>
    <!--c命名空间注入,通过构造器注入,constructor-->
    <bean id="user2" class="com.peng.pojo.User" c:age="18" c:name="peng"/>
</beans>

测试类

import com.peng.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        test2();
        test3();
    }

    @Test
    public static void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user",User.class);
        System.out.println(user);
    }

    @Test
    public static void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2",User.class);
        System.out.println(user);
    }
}

6.4 bean的作用域

  1. 单例模式 singleton

  1. 原型模式 prototype

    每次从容器中get的时候,都会产生一个新对象

  1. 其余的 request、session、application,这些只能在web开发中使用到
posted @ 2020-07-10 15:18  鹏懿如斯  阅读(433)  评论(0编辑  收藏  举报