Spring除了能使用@Value
把properties配置設定值注入到成員變數中外,也可透過@ConfigurationProperties
將properties檔內容綁定到Bean類的屬性。
範例環境:
- Java 8
- Spring Boot 2.3.2.RELEASE
- Maven
- Lombok
system.properites
在Maven專案的src/main/resources
classpath目錄,為要注入bean的properties檔。
system.properties
system.name=Demo system
system.version=1.0.0
system.url=192.168.0.111
system.port=8080
注入system.properites
內容的bean,在類別名稱前加上@ConfigurationProperties
,屬性prefix
可設定key的前綴。
SystemProperties
package com.abc.demo.properties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Getter
@Setter
@ConfigurationProperties(prefix = "system")
@Component
public class SystemProperties {
private String name; // system.name
private String version; // system.version
private String url; // system.url
private String port; // system.port
}
記得在配置類設定@PropertySource
指定classpath下的system.properties
。
DemoApplication
package com.abc.demo;
import com.abc.demo.properties.SystemProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
@PropertySource("classpath:system.properties")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
SystemProperties systemProperties = ctx.getBean(SystemProperties.class);
System.out.println(systemProperties.getName());
System.out.println(systemProperties.getVersion());
System.out.println(systemProperties.getUrl());
System.out.println(systemProperties.getPort());
// 菜鳥工程師肉豬
}
}
啟動專案印出結果。
Demo system
1.0.0
192.168.0.111
8080
SystemProperties
除了用一般的@Compoment
註冊為bean外,可改用在配置類加上@EnableConfigurationProperties
注釋來註冊指定的類,例如修改如下。
DemoApplication
package com.abc.demo;
import com.abc.demo.properties.SystemProperties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
...
@EnableConfigurationProperties(SystemProperties.class) // 註冊SystemProperties為bean
@PropertySource("classpath:system.properties")
@SpringBootApplication
public class DemoApplication {
...
}
由@EnableConfigurationProperties
指定的類不能同時使用@Compoment
,否則啟動時會出現
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.abc.demo.properties.SystemProperties' available: expected single matching bean but found 2: systemProperties,system-com.abc.demo.properties.SystemProperties
錯誤。
SystemProperties
package com.abc.demo.properties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Getter
@Setter
@ConfigurationProperties(prefix = "system")
//@Component // <-- @EnableConfigurationProperties指定的類不能同時使用@Compoment
public class SystemProperties {
private String name; // system.name
private String version; // system.version
private String url; // system.url
private String port; // system.port
}
參考github。
沒有留言:
張貼留言