Bootstrap

Spring基础04

Spring基础04

依赖注入

  1. 依赖注入:bean对象的创建依赖于容器,bean对象的所有属性,由容器来注入!

    • 构造器注入:

      • 使用无参构造方法创建。

        <bean id="hello" class="Hello">
        </bean>
        
    • 实体类中有有参构造方法,使用有参构造方法创建。

      • 下标方式:

         <!-- 有参构造  下标方式-->
        <bean id="hello" class="Hello">
           <constructor-arg index="0" value="hello"/>
        </bean>
        
      • type方式 (类型重复,不推荐):

            <!-- 有参构造  type方式-->
        <bean id="hello" class="Hello">
           <constructor-arg type="java.lang.String" value="hello"/>
        </bean>
        
      • 直接通过参数名:

        <!-- 有参构造  通过参数名-->    
        <bean id="hello" class="Hello">
          <!-- 如果参数是对象,就使用ref -->    
          <constructor-arg name="str" value="hello"/>
        </bean>
        
  2. Set方法注入:

    • 编写实体类:

      import java.util.*;
      
      public class Student {
      
          private String name;
          private Address address;
          private List<String> hobbies;
          private String[] books;
          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 List<String> getHobbies() {
              return hobbies;
          }
      
          public void setHobbies(List<String> hobbies) {
              this.hobbies = hobbies;
          }
      
          public String[] getBooks() {
              return books;
          }
      
          public void setBooks(String[] books) {
              this.books = books;
          }
      
          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 +
                      ", hobbies=" + hobbies +
                      ", books=" + Arrays.toString(books) +
                      ", card=" + card +
                      ", games=" + games +
                      ", wife='" + wife + '\'' +
                      ", info=" + info +
                      '}';
          }
      }
      
      public class Address {
          
          public Address() {
          }
      
          public Address(String address) {
              this.address = 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 + '\'' +
                      '}';
          }
      }
      
    • 编写Spring配置类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="Address">
              <property name="address" value="苏州"/>
          </bean>
      
          <bean id="student" class="Student">
              <!-- 第一种:普通值注入,value-->
              <property name="name" value="张三"/>
      
              <!-- 第二种:bean注入,ref-->
              <property name="address" ref="address"/>
      
              <!-- 第三种:数组注入-->
              <property name="books">
                  <array>
                      <value>book1</value>
                      <value>book2</value>
                      <value>book3</value>
                  </array>
              </property>
      
              <!-- 第四种:集合注入-->
              <property name="hobbies">
                  <list>
                      <value>singing</value>
                      <value>coding</value>
                      <value>playing basketball</value>
                  </list>
              </property>
      
              <!-- 第五种:Map注入-->
              <property name="card">
                  <map>
                      <entry key="idCard" value="123456"/>
                      <entry key="bankCard" value="123456"/>
                  </map>
              </property>
      
              <!-- 第六种:Set注入-->
              <property name="games">
                  <set>
                      <value>LOL</value>
                      <value>COC</value>
                  </set>
              </property>
      
              <!-- 第七种:null注入-->
              <property name="wife">
                 <null/>
              </property>
      
              <!-- 第八种:property注入-->
              <property name="info">
                  <props>
                      <prop key="学号">01</prop>
                      <prop key="性别"></prop>
                  </props>
              </property>
          </bean>
      
      </beans>
      
    • 编写测试类:

      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class Test {
      
          public static void main(String[] args) {
              ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
              Student student = (Student)context.getBean("student");
              Address address = (Address)context.getBean("address");
              System.out.println(student); // 输出Student{name='张三', address=Address{address='苏州'}, hobbies=[singing, coding, playing basketball], books=[book1, book2, book3], card={idCard=123456, bankCard=123456}, games=[LOL, COC], wife='null', info={学号=01, 性别=男}}
              System.out.println(address); // 输出Address{address='苏州'}
          }
      }
      
    • 拓展方法注入:P命名空间,C命名空间

      • Spring配置文件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"
               xmlns:p="http://www.springframework.org/schema/p"
               xmlns:c="http://www.springframework.org/schema/c">  <!-- 引入P命名空间,C命名空间约束 -->
        
            <!-- P命名空间注入。可以直接注入属性的值,property -->
            <bean id="address" class="Address" p:address="苏州">
            </bean>
        
            <!-- C命名空间注入。通过构造器注入, constructor-args-->
            <bean id="address1" class="Address" c:address="南京">
            </bean>
        
        </beans>
        
      • 编写测试类:

        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class Test {
        
            public static void main(String[] args) {
                ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
                Address address = (Address)context.getBean("address");
                Address address1 = (Address)context.getBean("address1");
                System.out.println(address); //输出Address{address='苏州'}
                System.out.println(address1); //输Address{address='南京'}
            }
        }
        
      • 注意点:P命名空间和C命名空间不能直接使用,需要导入xml约束!

        <?xml version="1.0" encoding="UTF-8"?>
        <beans
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c">
        </beans>
        
;