最佳实践是在哪里放置@EnableConfigurationProperties注解?

huangapple go评论102阅读模式
英文:

What is the best practice for placing an @EnableConfigurationProperties annotation?

问题

我有一个 SpringBoot 应用程序,其中有一些附加的属性类带有 @ConfigurationProperties 注解,还有一些配置类带有 @Configuration 注解。

@EnableConfigurationProperties 注解最佳放置位置在哪里?我看到它放在了主类上方,但放在配置类上方,即属性类的使用者,似乎是更好的放置位置,对我来说更有意义。关于放置这个注解,有什么最佳实践吗?

例如:

@SpringBootApplication
//是放在这里吗:
//@EnableConfigurationProperties(MyConfigProperties::class)
class MyApplication

fun main(args: Array<String>) {
  runApplication<MyApplication>(*args)
}

@Configuration
//还是放在这里?
//@EnableConfigurationProperties(MyConfigProperties::class)
class MyConfig(
    private val props: MyConfigProperties
){

    // 一些使用 props 的方法
}
@ConstructorBinding
@ConfigurationProperties("props")
data class MyConfigProperties(val myProp: String)
英文:

I have a SpringBoot application, with some additional properties classes annotated with @ConfigurationProperties, and also, I have some config. classes annotated with @Configuration.

What is the best place where to place @EnableConfigurationProperties annotation? I saw it above the main class, but above the config. class, which is a consumer of properties class, seems like better placement and make more sense to me. What is the best practice for placing this annotation?

e.g:

@SpringBootApplication
//should it be here:
//@EnableConfigurationProperties(MyConfigProperties::class)
class MyApplication

fun main(args: Array&lt;String&gt;) {
  runApplication&lt;MyApplication&gt;(*args)
}

    

@Configuration
//or here?
//@EnableConfigurationProperties(MyConfigProperties::class)
class MyConfig(
    private val props: MyConfigProperties
){

    // some methods, that uses props
}



@ConstructorBinding
@ConfigurationProperties(&quot;props&quot;)
data class MyConfigProperties(val myProp: String)

答案1

得分: 1

根据我的经验,你应该将注释放在使用这些属性的配置类中。

英文:

From my experience, you should put the annotation in the Configuration class that uses those properties.

答案2

得分: 1

@ConfigurationProperties 允许开发者将整个 .properties.yml 文件映射到一个对象中,以便轻松操作。

@Configuration 表示该类可以被 Spring IoC 容器用作 bean 定义的来源。

@ConfigurationProperties 的一个重要优势可以是 外部化配置

文档中提到:

Spring Boot 允许您将配置外部化,以便在不同环境中使用相同的应用程序代码。您可以使用属性文件、YAML 文件、环境变量和命令行参数来外部化配置。属性值可以通过使用 @Value 注解直接注入到您的 bean 中,通过 Spring 的环境抽象访问,或者通过 @ConfigurationProperties 绑定到结构化对象中。

英文:

Those annotations have different purposes.

@ConfigurationProperties is letting developer maps the entire .properties and .yml file into an object easily.

@Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions.

One big advantage of @ConfigurationPropertiescan be Externalized Configuration

Documentation says:
> Spring Boot lets you externalize your configuration so that you can
> work with the same application code in different environments. You can
> use properties files, YAML files, environment variables, and
> command-line arguments to externalize configuration. Property values
> can be injected directly into your beans by using the @Value
> annotation, accessed through Spring’s Environment abstraction, or be
> bound to structured objects through @ConfigurationProperties.

huangapple
  • 本文由 发表于 2020年9月2日 20:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63705409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定