網頁

2019/8/31

Spring Boot properties file value injection 屬性配置檔注入

Spring Boot properties配置檔參數注入範例。

範例環境:

  • Windows 64 Bit
  • Java 8
  • Eclipse Version: 2019-03 (4.11.0)
  • Spring Boot版本2.1.6.RELEASE
  • Maven

在專案的classpath,src/main/resources新增兩個properties檔:

  • my-message.properties
  • other-message.properties

注意以下properties檔皆在Eclipse設為UTF-8編碼。

my-message.properties

my.name=松園別館
my.address=花蓮縣花蓮市松園街65號
my.postcode=97059
my.ticket-price=60

other-message.properties

other.title=其他${my.name}
other.amount=10000

${my.name}placeholders,用來表示其他properties定義的參數值,
也就是my-messages中的my.name=松園別館


在Spring Boot專案的@SprinbBootApplication所在類別DemoApplication使用@PropertySourcevalue的值為字串陣列,可設定多個要注入的配置檔位置及名稱;
ignoreResourceNotFound的值為boolean,設定忽略找不到的參數;
encodeing的值為字串,用來設定編碼。這邊設為UTF-8是因為上面的properties也是UTF-8

main()中透過ApplicationContext.getBean()取得注入properties參數值的Bean物件MyMessagesOtherMessages

DemoApplication

package com.abc.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;

import com.abc.demo.message.MyMessages;
import com.abc.demo.message.OtherMessages;

@SpringBootApplication
@PropertySource(
    value = { "classpath:my-messages.properties", "classpath:other-messages.properties" }, 
    ignoreResourceNotFound = true,
    encoding = "UTF-8"
)
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext appCtx = SpringApplication.run(DemoApplication.class, args);
        
        MyMessages myMessages = appCtx.getBean(MyMessages.class);
        OtherMessages otherMessages = appCtx.getBean(OtherMessages.class);
        
        myMessages.printPropertiesValue();
        
        System.out.println("-----------------------");
        
        otherMessages.printPropertiesValue();
    }

}

MyMessages例用@Value直接注入properties參數值至成員變數

MyMessages

package com.abc.demo.message;

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

@Component
public class MyMessages {

    @Value("${my.name}")
    private String name;
    @Value("${my.address}")
    private String address;
    @Value("${my.post-code}")
    private String postCode;
    @Value("${my.ticket-price}")
    private int ticketPrice;

    public void printPropertiesValue() {
        System.out.println(name);
        System.out.println(address);
        System.out.println(postCode);
        System.out.println(ticketPrice);
    }

    // getter setter...
}

OtherMessages則透過Spring的Environment來取得properties的值。

OtherMessages

package com.abc.demo.message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class OtherMessages {

    @Autowired
    private Environment env;

    public void printPropertiesValue() {
        System.out.println(env.getProperty("other.title"));
        System.out.println(env.getProperty("other.amount"));
    }

}

啟動Spring Boot專案會印出下面結果

松園別館
花蓮縣花蓮市松園街65號
97059
60
-----------------------
其他松園別館
10000

範例完成後的專案目錄結構如下。




參考:

沒有留言:

張貼留言