简体   繁体   English

@ConfigurationProperties vs @PropertySource vs @Value

[英]@ConfigurationProperties vs @PropertySource vs @Value

I am new to Spring/Spring Boot.我是 Spring/Spring Boot 的新手。 I want to use the key-value pair data of application.properties / application.yml in Java file.我想application.yml Java文件中application.properties的键值对数据。 I know that we can use @Value in any POJO class to set a default value of a field from application.properties or application.yml file.我知道我们可以在任何 POJO class 中使用@Value来设置application.propertiesapplication.yml文件中字段的默认值。

Q1) But then why do we need the other two? Q1)但是为什么我们需要另外两个呢? @ConfigurationProperties and @PropertySource . @ConfigurationProperties@PropertySource

Q2) @ConfigurationProperties and @PropertySource , both can be used to load external data mentioned in application.properties or application.yml file? Q2) @ConfigurationProperties@PropertySource都可以用来加载application.propertiesapplication.yml文件中提到的外部数据吗? Or any restrictions?或者有什么限制?

PS: I have tried to search on internet but didn't get a clear answer. PS:我尝试在互联网上搜索,但没有得到明确的答案。

@ConfigurationProperties is used to map properties with POJO beans. @ConfigurationProperties用于带有 POJO bean 的 map 属性。 Then you could use bean to access the properties values in your application.然后您可以使用 bean 访问应用程序中的属性值。

@PropertySource is to reference a properties file and load it. @PropertySource是引用一个属性文件并加载它。

@Value is to inject a particular property value by it's key. @Value是通过它的键注入特定的属性值。

@Value("${spring.application.name}") @Value will throw exception if there no matching key in application.properties/yml file. @Value("${spring.application.name}")如果 application.properties/yml 文件中没有匹配的键,@Value 将抛出异常。 It strictly injects property value.它严格注入财产价值。

For example: @Value("${spring.application.namee}") throws below exception, as namee field doesn't exists in properties file.例如: @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") injects POJO properties, it's not strict, it ignores the property if there is no key in properties file. @ConfigurationProperties(prefix = "myserver.allvalues")注入 POJO 属性,它并不严格,如果属性文件中没有键,它会忽略该属性。

For example:例如:

@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

Base on my research & understanding::根据我的研究和理解::

  • @ConfigurationProperties

    loads properties from application.propertiesapplication.properties加载属性

    you specify the field names to correspond to the names of the properties in application.properties您指定字段名称以对应于application.properties中的属性名称

    -- @ConfigurationProperties does not work with @Value -- @ConfigurationProperties不适用于@Value

  • @PropertySource

    loads properties from a file you specify从您指定的文件加载属性

    can use with @Value or @Autowired Environment env;可以与@Value@Autowired Environment env;

  • @Value

    it is used with application.properties它与application.properties一起使用

    application.properties is loaded by default (you dont need to specify in @PropertySource ) application.properties默认加载(您不需要在@PropertySource中指定)


Reference参考

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

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

- -

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment: SpringApplication 将从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

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

- -

@ConfigurationProperties annotation. @ConfigurationProperties 注释。 When placed on any Spring bean, it specifies that the properties of that bean can be injected from properties in the Spring environment.当放置在任何 Spring bean 上时,它指定可以从 Spring 环境中的属性注入该 bean 的属性。

< Spring In Action > < Spring 在行动 >

- -

You can bundle the configuration file in your application jar or put the file in the filesystem of the runtime environment and load it on Spring Boot startup.您可以将配置文件捆绑在您的应用程序 jar 中或将文件放入运行时环境的文件系统中,并在 Spring 启动时加载。

Spring Boot loads the application.properties file automatically from the project classpath. Spring Boot 会自动从项目类路径加载 application.properties 文件。

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

- -

4.1. 4.1。 application.properties: the Default Property File application.properties:默认属性文件

Boot applies its typical convention over configuration approach to property files. Boot 将其典型的约定优于配置方法应用于属性文件。 This means that we can simply put an application.properties file in our src/main/resources directory, and it will be auto-detected.这意味着我们可以简单地将 application.properties 文件放在我们的 src/main/resources 目录中,它将被自动检测到。 We can then inject any loaded properties from it as normal.然后我们可以像往常一样从中注入任何加载的属性。

So, by using this default file, we don't have to explicitly register a PropertySource or even provide a path to a property file.因此,通过使用此默认文件,我们不必显式注册 PropertySource,甚至不必提供属性文件的路径。

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

- -

@ConfigurationProperties indicates to spring that it should bind the java fields based on their name to some matching properties. @ConfigurationProperties 向 spring 指示它应该根据名称将 java 字段绑定到某些匹配属性。

spring requires that the class that has this annotation must be a spring bean spring 要求具有此注解的 class 必须是 spring bean

Spring Inject Values with @ConfigurationProperties and @Value Spring 使用@ConfigurationProperties 和@Value 注入值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PropertiesFactoryBean与@PropertySource - PropertiesFactoryBean vs @PropertySource 如何构建自定义PropertySource以绑定到@ConfigurationProperties - How to build custom PropertySource for binding to @ConfigurationProperties Springboot中使用@ConfigurationProperties和@PropertySource时如何将属性序列化为对象? - In Springboot, how to serialize properties into objects when using @ConfigurationProperties and @PropertySource? 如何在 Spring Boot 2.2.4 中将 @ConstructorBinding 和 @PropertySource 与 @ConfigurationProperties 一起使用? - How to use @ConstructorBinding and @PropertySource together with @ConfigurationProperties with in Spring Boot 2.2.4? Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + 属性源文件中的列表未注入 - Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + List in Property Source file not injecting Spring @PropertySource 值不会被覆盖 - Spring @PropertySource value not overridding 在运行时更改@PropertySource值 - Change @PropertySource value in run time @PropertySource是否可以用于具有@Value的属性? - Is it possible for @PropertySource to be used for properties with @Value? 我正在使用 @PropertySource 和 @ConfigurationProperties 的组合,但我想用外部属性文件覆盖它们 - I am using a combination of @PropertySource and @ConfigurationProperties but I want to overwrite them with an external properties file Spring 使用@ConfigurationProperties 和@Value 注入值 - Spring Inject Values with @ConfigurationProperties and @Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM