@Value("${spring.application.name}")
如果application.properties/yml文件中没有匹配的键,则@Value将引发异常。严格注入属性值。
例如:@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属性,这并不严格,如果属性文件中没有键,它将忽略该属性。
例如:
@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
,
@ConfigurationProperties
用于通过POJO bean映射属性。然后,您可以使用bean访问应用程序中的属性值。
@PropertySource
是引用属性文件并加载它。
@Value
是通过其键注入特定的属性值。
本文链接:https://www.f2er.com/3167591.html