**set注入** 构造器注入

DI依赖注入

  1. 构造器注入

  2. set注入

    • 依赖注入:Set注入
      • 依赖:bean对象的创建依赖于容器
      • 注入:bean对象的所有属性依赖于注入
    1. 复杂类型

      public class Address {
          public String address;
          public String getAddress() {
              return address;
          }
          public void setAddress(String address) {
              this.address = address;
          }
      }
      
    2. 真实测试对象

      package com.wjj.pojo;
      
      import java.util.*;
      
      public class Student {
          private Address address;
          private String[] books;
          private String name;
          private Set<String> games;
          private String wife;
          private List<String> hobbies;
          private Properties info;
          private Map<String,String> card;
      
          public Address getAddress() {
              return address;
          }
      
          public void setAddress(Address address) {
              this.address = address;
          }
      
          public String[] getBooks() {
              return books;
          }
      
          public void setBooks(String[] books) {
              this.books = books;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          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 List<String> getHobbies() {
              return hobbies;
          }
      
          public void setHobbies(List<String> hobbies) {
              this.hobbies = hobbies;
          }
      
          public Properties getInfo() {
              return info;
          }
      
          public void setInfo(Properties info) {
              this.info = info;
          }
      
          public Map<String, String> getCard() {
              return card;
          }
      
          public void setCard(Map<String, String> card) {
              this.card = card;
          }
      
          @Override
          public String toString() {
              return "Student{" +
                      "address=" + address +
                      ", books=" + Arrays.toString(books) +
                      ", name='" + name + '\'' +
                      ", games=" + games +
                      ", wife='" + wife + '\'' +
                      ", hobbies=" + hobbies +
                      ", info=" + info +
                      ", card=" + card +
                      '}';
          }
      }
      
    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="student" class="com.wjj.pojo.Student">
              <!--普通注入    -->
              <property name="name" value="wjj">
              </property>
              <!--bean注入        -->
              <property name="address" ref="address">
              </property>
              <!--数组        -->
              <property name="books" >
                  <array>
                      <value>挪威的森林</value>
                      <value>月亮与六便士</value>
                  </array>
              </property>
              <!--列表        -->
              <property name="hobbies">
                  <list>
                      <value>追番</value>
                  </list>
              </property>
              <!--map        -->
              <property name="card">
                  <map>
                      <entry key="身份证" value="111111">
      
                      </entry>
                  </map>
              </property>
              <!--set   -->
              <property name="games">
              <set>
                  <value>lol</value>
              </set>
              </property>
              <!--Null        -->
              <property name="wife">
                  <null/>
              </property>
              <property name="info">
                  <props >
                      <prop key="学号">577</prop>
                  </props>
              </property>
          </bean>
          <bean id="address" class="com.wjj.pojo.Address">
      
          </bean>
      
      </beans>
      
    4. 测试类

      public class StudentTest {
          @Test
          public void getName(){
              ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
              com.wjj.pojo.Student student =(Student) context.getBean("student");
              System.out.println(student.toString());
      
          }
      
      }
      
  3. 扩展方式

    1. 用p命名空间 放在对应的xml‘文件中

      xmlns:p="http://www.springframework.org/schema/p"
      <bean id="student2" class="com.wjj.pojo.Student" p:age="21" p:alias="jwj"></bean>
      
    2. 用c命名空间

      xmlns:c="http://www.springframework.org/schema/c"
      <bean id="student3" class="com.wjj.pojo.Student" c:age="21" c:alias="577"></bean>
      
      

posted @ 2026-03-17 20:36  三咲肘击王  阅读(5)  评论(0)    收藏  举报