Bean 的裝配方式

Bean 的裝配方式

概述

截止目前爲止,咱們 Bean 的裝配方式是通過代碼 getBean() 的方式從容器獲取指定的 Bean 實例,容器首先會調用 Bean 類的無參構造器,創建空值的實例對象。除了使用 getBean() 的裝配方式外,還可以使用註解的裝配方式。

容器中 Bean 的作用域

在學習 Bean 的裝配方式之前,我們先了解一下 Bean 的作用域。當通過 Spring 容器創建一個 Bean 實例時,不僅可以完成 Bean 的實例化,還可以通過 scope 屬性,爲 Bean 指定特定的作用域。Spring 支持 5 種作用域。

  • singleton:單態模式。即在整個 Spring 容器中,使用 singleton 定義的 Bean 將是單例的,只有一個實例。默認爲單態的。
  • prototype:原型模式。即每次使用 getBean 方法獲取的同一個 <bean /> 的實例都是一個新的實例。
  • request:對於每次 HTTP 請求,都將會產生一個不同的 Bean 實例。
  • session:對於每個不同的 HTTP session,都將產生一個不同的 Bean 實例。
  • global session:每個全局的 HTTP session 對應一個 Bean 實例。典型情況下,僅在使用 portlet 集羣時有效,多個 Web 應用共享一個 session。一般應用中,global-session 與 session 是等同的。

注意事項:

  • 對於 scope 的值 request、session 與 global session,只有在 Web 應用中使用 Spring 時,該作用域纔有效。
  • 對於 scope 爲 singleton 的單例模式,該 Bean 是在容器被創建時即被裝配好了。
  • 對於 scope 爲 prototype 的原型模式,Bean 實例是在代碼中使用該 Bean 實例時才進行裝配的。

基於註解的裝配方式

對於 DI 使用註解,將不再需要在 Spring 配置文件(spring-context.xml)中聲明 Bean 實例。Spring 中使用註解, 需要在原有 Spring 運行環境基礎上再做一些改變

需要在 Spring 配置文件中配置組件掃描器,用於在指定的基本包中掃描註解。

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

    <context:annotation-config />
    <context:component-scan base-package="com.funtl.leeshop"/>
</beans>

 @Component

需要在類上使用註解 @Component,該註解的 value 屬性用於指定該 bean 的 id 值。

@Component(value = "student")
public class Student {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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

Spring 還提供了 3 個功能基本和 @Component 等效的註解:

  • @Repository:用於對 DAO 實現類進行註解
  • @Service:用於對 Service 實現類進行註解
  • @Controller:用於對 Controller 實現類進行註解

@Scope

需要在類上使用註解 @Scope,其 value 屬性用於指定作用域。默認爲 singleton。

 @Value

需要在屬性上使用註解 @Value,該註解的 value 屬性用於指定要注入的值。

使用該註解完成屬性注入時,類中無需 setter。當然,若屬性有 setter,則也可將其加到 setter 上。

 @Autowired

需要在域屬性上使用註解 @Autowired,該註解默認使用 按類型自動裝配 Bean 的方式。

使用該註解完成屬性注入時,類中無需 setter。當然,若屬性有 setter,則也可將其加到 setter 上。

@Resource

需要在域屬性上使用註解 @Resource,該註解有一個 name 屬性,可以創建指定的 bean

@Resource(name = "userService")
private UserService userService;

@PostConstruct

在方法上使用 @PostConstruct 相當於初始化

註解與 XML 配置的區別

註解的好處是,配置方便,直觀。但其弊端也顯而易見:以硬編碼的方式寫入到了 Java 代碼中,其修改是需要重新編譯代碼的。

XML 配置方式的最大好處是,對其所做修改,無需編譯代碼,只需重啓服務器即可將新的配置加載。

若註解與 XML 同用,XML 的優先級要高於註解。這樣做的好處是,需要對某個 Bean 做修改,只需修改配置文件即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章