天天看點

Spring入門學習四:自動裝配

自動裝配

在前面幾個例子中,每一個參數都是我們自己手動裝配的,但是spring提供了滿足bean依賴的一種方式,它會在上下文中自動尋找,并給Bean裝配屬性。

spring的三種裝配方式

  • xml中顯示的配置
  • 隐式的自動裝配【重點】
  • 在java中顯示配置【了解】

環境搭建

可以參考前面的部落格。

實體

package com.pojo;

public class Phone {
    public void playGame() {
        System.out.println("用手機打遊戲");
    }
}

           
package com.pojo;

public class Computer {
    public void playGame() {
        System.out.println("用電腦打遊戲");
    }
}

           
package com.pojo;

public class People {
    private String name;
    Phone phone;
    Computer computer;

    public String getName() {
        return name;
    }

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

    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public Computer getComputer() {
        return computer;
    }

    public void setComputer(Computer computer) {
        this.computer = computer;
    }
}

           

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 = "phone" class="com.pojo.Phone"></bean>
    <bean id = "computer" class="com.pojo.Computer"></bean>
	
    <!--
    autowired:自動裝配,這裡是通過byName。即尋找上下文跟set方法後面的bean相比對的值
    -->
    <bean id = "people" class="com.pojo.People" autowire="byName">
        <property name="name" value="String"/>
    </bean>
</beans>
           

測試

package com;

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

public class PeopleTest {

    @Test
    public void test() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        People people = applicationContext.getBean("people", People.class);

        people.getPhone().playGame();
        people.getComputer().playGame();
    }
}

           
用手機打遊戲
用電腦打遊戲

Process finished with exit code 0
           

XML自動裝配

在bean标簽中有一個屬性 autowired,該屬性可以實作xml的自動裝配

  • byName:尋找上下文跟set方法後面的bean相比對的值。聲明的bean id必須全局唯一且跟自動注入屬性中的名字比對,即id唯一且比對!
  • byType:查找IOC容器裡面查找和自己對象屬性類型相同的bean。每個類隻能聲明一個bean,即class唯一!

注解自動裝配@Autowired

使用注解之前需要導入限制,context。

配置注解支援

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-autowired-annotation

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

    <context:annotation-config/>

</beans>
           

@Autowired

修改配置檔案,注意開啟注解支援

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

<!--
        開啟注解的支援不能漏
-->
    <context:annotation-config/>

    <bean id = "phone" class="com.pojo.Phone"/>
    <bean id = "computer" class="com.pojo.Computer"/>

    <bean id = "people" class="com.pojo.People" autowire="byName"/>
</beans>
           

修改people的實體類

@Autowired
    Phone phone;//自動注入phone
    @Autowired
    Computer computer;//自動注入Computer

           

一般在配置檔案中注冊了的類

在其他類的屬性需要注入的時候

,都可以用這個注解。還有一個使用的地方是用在set方法上。使用了這個注解,我們甚至可以不編寫這個被注入的屬性的set方法,前提是你這個需要用@Autowired的屬性已經在IOC容器中存在。這個注解最常用的地方是類的屬性。比如在Service注入Dao的對象,在Controller注入Service的對象。

擴充:

在Spring5之後,多了一個注解@Nullable。某個屬性被标記了這個注解,說明這個字段可以被注冊為空,類似的。

@Nullable
    private String name;

    @Autowired
    Phone phone;//自動注入phone
    @Autowired
    Computer computer;//
           
People{name='null', phon[email protected], [email protected]}
           

如果顯示定義了@Autowired的required 屬性為false(預設為true),說明這個對象可以為null。類似的

@Nullable
    private String name;

    @Autowired(required = false)
    Phone phone;//自動注入phone
    @Autowired
    Computer computer;//
           

@Qualifier。如果自動裝配的環境比較複雜,可以使用該注解,指定我們所需的IOC容器中的某個bean

@Nullable
    private String name;

    @Autowired(required = false)
    Phone phone;//自動注入phone
    @Autowired
    @Qualifier(value = "computer")//顯示的指定我們要取IOC容器裡面的bean
    Computer computer;//
           

@Autowired:通過類型名字 自動裝配,當情況複雜的時候,可以用@Qualifier(value = “beanid”)指定唯一一個bean注入。