简体   繁体   English

Spring - yaml 和.properties 配置

[英]Spring - yaml and .properties configurations

i've been working with Spring for some time and have a question regarding the very common configuration properties files (like the common application.properties that comes with every spring boot app you initialize).我已经使用 Spring 有一段时间了,并且对非常常见的配置属性文件有疑问(例如您初始化的每个 spring 启动应用程序附带的常见application.properties )。 Recently, i also found that configurations like this can be done in yaml files.最近,我还发现像这样的配置可以在 yaml 文件中完成。 I have two questions:我有两个问题:

  1. When in a application.properties file, we write something like:application.properties文件中,我们编写如下内容:
# application.properties

spring.debug = true
some-random-value = 15

does this mean that these values will be injected in the application context?这是否意味着这些值将被注入到应用程序上下文中?

  1. When we write something like:当我们写这样的东西时:
# application.properties

spring.debug = true

does this mean, that somewhere, there is some class, that has an attribute that looks something like this?这是否意味着,某处有一些 class,其属性看起来像这样? --> -->

@Component
class SomeClass{

@Value("spring.debug")
boolean shouldIRunInDebugMode;

...
}

2.a. 2.a. If the answer to question 2 is yes, then how can I, looking at something like this:如果问题 2 的答案是肯定的,那么我该如何看待这样的事情:

# application.properties

spring.debug = true

find that class that is expecting that value.找到期望该值的 class 。 The same would apply to if i was looking at something like:如果我在看类似的东西,这同样适用:

# application.yaml

someThidPartyLibraryName:
   shouldWeLog: true

If i see a yaml configuration file, just looking at all the configuration there usually is not enough for me to know what is happening.如果我看到 yaml 配置文件,仅查看所有配置通常不足以让我知道发生了什么。 How can i find the class that is being affected by this configuration, so that i can better understand what this config is doing?如何找到受此配置影响的 class,以便我更好地了解此配置在做什么?

Thank you!谢谢!

The response is generally yes.回答通常是肯定的。 If you declare a property in the application.properties or application.yaml is mainly, because you would use it later in the code, for example injecting in some bean with the support of @Value annotation.如果你在application.propertiesapplication.yaml中声明一个属性,主要是因为你会在后面的代码中使用它,例如注入一些支持@Value注解的bean。 However, there are also many built-in properties (let's say for example server.port ), which you usually don't have to declare and therefore you won't see explicitly in the code.但是,还有许多内置属性(例如server.port ),您通常不必声明它们,因此您不会在代码中明确看到。 Use an IDE to search the configuration properties and the manual to check the preconfigured ones in case of need.使用 IDE 搜索配置属性和手册以检查预配置的,以备不时之需。

Properties defined in yaml or a properties file may be accessed using the @Value annotation to inject, or using a @ConfigurationProperties class - see https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-typesafe-configuration-properties for complete details. Properties defined in yaml or a properties file may be accessed using the @Value annotation to inject, or using a @ConfigurationProperties class - see https://docs.spring.io/spring-boot/docs/current/reference/html/spring -boot-features.html#boot-features-external-config-typesafe-configuration-properties了解完整详情。

Finding the property usage is supported by some IDEs - IntelliJ allows you to click through.某些 IDE 支持查找属性使用情况 - IntelliJ 允许您单击。 Otherwise it's a search through the source.否则,它是通过源搜索。 For @ConfigurationProperties , once you find the class then just look for code that calls its accessor methods.对于@ConfigurationProperties ,一旦找到 class ,只需查找调用其访问器方法的代码即可。

Your understanding regarding spring value injections from application.properties is correct.您对来自 application.properties 的 spring 值注入的理解是正确的。 #2 - is Yes. #2 - 是的。 Any property from application.properties can be injected to any java class as @Value. application.properties 中的任何属性都可以作为@Value 注入到任何 java class 中。

Regarding #2.a - Yaml is just another format on how you organize your variable hierarchy by indentations.关于#2.a - Yaml 只是关于如何通过缩进组织变量层次结构的另一种格式。 That's a superset to the JSON structure.这是 JSON 结构的超集。

For example,例如,

in application.properties file you can add something like this myapp.db.url=<dburl> myapp.db.username=<dbuser> myapp.db.password=<dbpassword>在 application.properties 文件中,您可以添加类似myapp.db.url=<dburl> myapp.db.username=<dbuser> myapp.db.password=<dbpassword>

the same can be represented in Yaml in a much efficient manner as below myapp: db: url:<dburl> username:<dbuser> password:<dbpassword>同样可以在 Yaml 中以非常有效的方式表示,如下myapp: db: url:<dburl> username:<dbuser> password:<dbpassword>

And in either case, for your Jave file you can inject as无论哪种情况,对于您的 Jave 文件,您都可以将其注入为

@Value("myapp.db.url" private String dbUrl;

Properties files and yaml files are used in Spring Boot for configurations.属性文件和 yaml 文件在 Spring 引导中用于配置。 The main difference between the two is yaml provides structuring/grouping of configurations where as Properties are usually flat and may be repeating the same information:两者之间的主要区别是 yaml 提供了配置的结构化/分组,其中属性通常是扁平的,并且可能重复相同的信息:

For example;例如;

Properties file属性文件

server.port = 8080
server.host = localhost

yaml file yaml 文件

server:
  port: 8080
  host: localhost

But in a Spring Boot AutoConfiguration class regardless of yaml or Properties used, a following looking ConfigurationProperties class will be used which will map server.port and server.host But in a Spring Boot AutoConfiguration class regardless of yaml or Properties used, a following looking ConfigurationProperties class will be used which will map server.port and server.host

@ConfigurationProperties(prefix = "server")
public class ServerConfiguration {

    private int port;
    private String host;

}

@Configuration
@EnableConfigurationProperties(ServerConfiguration.class)
public class ServerAutoConfiguration {

}

Hope this answers your questions.希望这能回答你的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM