Spring屬性注入方式詳解(附源碼剖析)

一、什麼是Spring屬性注入

在Java中,萬物皆對象,屬性注入就是在實例化對象時,同時向對象中的屬性進行相應的賦值。通俗點說,屬性注入就是給類中的屬性賦值。

二、屬性注入的幾種方式

對於類成員變量來說,注入方式有三種:
1.setter方式注入
2.構造函數注入
3.接口注入

對於Spring來說,Spring支持前面兩種,並且還支持:
1.p名稱空間注入
2.spel屬性注入
3.複雜類型注入

三、屬性注入舉例

第一種:setter方式注入
1.創建一個SpringBoot項目
2.編寫一個Book.java類,如下:

package com.mango.properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
/*@PropertySource("classpath:book.properties")*/
public class Book {

    @Value("${book.id}")
    private long id;
    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;

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

    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;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

3.在創建SpringBoot項目時自動生成的application.properties中添加如下內容:

book.id=1
book.name=三國演義
book.author=羅貫中

4.編寫測試類PropertiesApplicationTests.java,如下:

package com.mango.properties;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class PropertiesApplicationTests {

    @Autowired
    Book book;

    @Test
    void contextLoads() {
        System.err.println(book);
    }

}

5.運行結果:

Book{id=1, name='三國演義', author='羅貫中'}

注意事項:
1.若使用自己創建的xxx.properties文件進行上述方式進行屬性注入(注入內容與上述第3步相同),需要在Book.java上進行添加如下註解:

@PropertySource("classpath:book.properties")

注:此註解就是指定自己創建的xxx.properties文件

完整代碼如下所示:

package com.mango.properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:book.properties")
public class Book {

    @Value("${book.id}")
    private long id;
    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;

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

    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;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章