@ConfigurationProperties vs @PropertySource vs @Value



我是Spring/Spring Boot的新手。我想在Java文件中使用application.properties/application.yml的键值对数据。我知道我们可以在任何 POJO 类中使用@Value来设置application.propertiesapplication.yml文件中字段的默认值。

问题 1)但是,为什么我们需要另外两个呢?@ConfigurationProperties@PropertySource.

问题 2)@ConfigurationProperties@PropertySource,都可以用来加载application.propertiesapplication.yml文件中提到的外部数据?还是任何限制?

PS:我尝试在互联网上搜索,但没有得到明确的答案。

@ConfigurationProperties用于 POJO bean 以将属性映射到其字段或资源库。然后,可以使用 Bean 访问应用程序逻辑中的属性值。

@PropertySource是引用一个属性文件并将其加载到 Spring 环境中(@ConfigurationProperties或@Value可以使用它)。

@Value是通过键将特定属性值注入变量(成员字段或构造函数参数)。

如果application.properties/yml文件中没有匹配的键,@Value("${spring.application.name}")@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

    application.properties加载属性

    指定字段名称以对应于application.properties中的属性名称

    --@ConfigurationProperties不适用于@Value

  • @PropertySource

    从指定的文件加载属性

    可与@Value@Autowired Environment env;一起使用

  • @Value

    它与application.properties一起使用

    默认情况下加载application.properties(无需在@PropertySource中指定)

<小时 />

参考

https://mkyong.com/spring-boot/spring-boot-configurationproperties-example/

https://mkyong.com/spring/spring-propertysources-example/

-

SpringApplication 将从以下位置的 application.properties 文件中加载属性,并将它们添加到 Spring 环境中:

https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/boot-features-external-config.html

-

@ConfigurationProperties注释。当放置在任何 Spring Bean 上时,它指定可以从 Spring 环境中的属性注入该 Bean 的属性。

<春天在行动>

-

您可以将配置文件捆绑在应用程序 jar 中,或者将该文件放在运行时环境的文件系统中,并在 Spring 引导启动时加载它。

Spring 引导会自动从项目类路径加载 application.properties 文件。

http://dolszewski.com/spring/spring-boot-application-properties-file/

-

4.1. 应用程序属性:默认属性文件

Boot 将其典型的配置约定应用于属性文件。这意味着我们可以简单地将一个 application.properties 文件放在我们的 src/main/resources 目录中,它将被自动检测。然后,我们可以正常地从中注入任何加载的属性。

因此,通过使用此默认文件,我们不必显式注册 PropertySource,甚至不必提供属性文件的路径。

https://www.baeldung.com/properties-with-spring

-

@ConfigurationProperties 向 spring 指示它应该根据 java 字段的名称将它们绑定到一些匹配的属性。

Spring要求具有此注释的类必须是 Spring Bean

弹簧注入具有@ConfigurationProperties和@Value

的值

最新更新