@ConfigurationProperties与@PropertySource与@Value的区别

27

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

问题1:那么我们为什么还需要另外两个注释@ConfigurationProperties@PropertySource呢?

问题2: @ConfigurationProperties@PropertySource都可以用于加载application.propertiesapplication.yml文件中提到的外部数据吗?有任何限制吗?

附:我尝试在互联网上搜索,但没有得到清晰的答案。


1
你可以在这里查看差异 https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/ - vivekdubey
3个回答

23

@ConfigurationProperties用于将属性映射到POJO bean的字段或setter方法。然后,您可以在应用程序逻辑中使用该bean来访问属性值。

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

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


20

@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

4
@propertySource是什么意思呢? - Surya

-1

根据我的研究和理解:

  • @ConfigurationProperties

    application.properties中加载属性

    您可以指定字段名称与application.properties中属性的名称相对应

    -- @ConfigurationProperties不能与@Value一起使用

  • @PropertySource

    从指定的文件中加载属性

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

  • @Value

    它与application.properties一起使用

    application.properties默认加载(您不需要在@PropertySource中指定)


参考资料
Spring Boot 配置属性示例:https://mkyong.com/spring-boot/spring-boot-configurationproperties-example/
Spring 属性源示例: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 上时,它指定该 bean 的属性可以从 Spring 环境中的属性中注入。 < Spring In Action > 您可以将配置文件捆绑在应用程序 jar 中,也可以将文件放在运行时环境的文件系统中,并在 Spring Boot 启动时加载它。
Spring Boot 会自动从项目类路径加载 application.properties 文件。
http://dolszewski.com/spring/spring-boot-application-properties-file/
4.1. application.properties:默认属性文件
Boot 将其典型的约定优于配置方法应用于属性文件。这意味着我们只需将 application.properties 文件放在 src/main/resources 目录中,它就会被自动检测到。然后,我们可以像平常一样注入任何已加载的属性。
因此,通过使用此默认文件,我们不必显式注册 PropertySource,甚至不必提供属性文件的路径。
https://www.baeldung.com/properties-with-spring
@ConfigurationProperties 指示 Spring 应根据名称将 java 字段绑定到某些匹配属性。
Spring 要求具有此注释的类必须是 Spring bean。
使用 @ConfigurationProperties 和 @Value 在 Spring 中注入值: Spring Inject Values with @ConfigurationProperties and @Value

@ConfigurationProperties@Value不仅可以从application.properties中加载,还可以从此页面顶部列出的所有17个源中加载:https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html - Alexander Taylor
这个答案让人觉得注解之间的区别在于它加载的文件不同。但是主要的区别不仅如此,正如本页面上的其他答案所指出的那样。 - Alexander Taylor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接