3 回答

TA貢獻1772條經(jīng)驗 獲得超5個贊
@ConfigurationProperties
用于 POJO bean 將屬性映射到其字段或設(shè)置器。然后,您可以使用該 bean 來訪問應(yīng)用程序邏輯中的屬性值。
@PropertySource
是引用一個屬性文件并將其加載到Spring環(huán)境中(其中可以被@ConfigurationProperties或@Value使用)。
@Value
是將特定屬性值通過其鍵注入到變量(成員字段或構(gòu)造函數(shù)參數(shù))中。

TA貢獻1887條經(jīng)驗 獲得超5個贊
@Value("${spring.application.name}") 如果 application.properties/yml 文件中沒有匹配的鍵,@Value 將拋出異常。它嚴格注入財產(chǎn)價值。
例如:@Value("${spring.application.namee}")拋出以下異常,因為namee屬性文件中不存在字段。
application.properties file
----------------------
spring:
application:
name: myapplicationname
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.namee' in value "${spring.application.namee}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.namea' in value "${spring.application.namee}"
@ConfigurationProperties(prefix = "myserver.allvalues")注入 POJO 屬性,不嚴格,如果屬性文件中沒有 key 則忽略該屬性。
例如:
@ConfigurationProperties(prefix = "myserver.allvalues")
public class TestConfigurationProperties {
private String value;
private String valuenotexists; // This field doesn't exists in properties file
// it doesn't throw error. It gets default value as null
}
application.properties file
----------------------
myserver:
allvalues:
value: sampleValue

TA貢獻1770條經(jīng)驗 獲得超3個贊
基于我的研究和理解::
@ConfigurationProperties
從加載屬性
application.properties
您指定字段名稱以對應(yīng)于中的屬性名稱
application.properties
--
@ConfigurationProperties
不適用于@Value
@PropertySource
從您指定的文件加載屬性
可以與
@Value
或 一起使用@Autowired Environment env;
@Value
它與
application.properties
application.properties
默認加載(不需要在 中指定@PropertySource
)
添加回答
舉報