【问题标题】:SpringBoot difference between @Configuration and @configurationProperties@Configuration 和 @configurationProperties 之间的 Spring Boot 区别
【发布时间】:2018-10-30 20:04:54
【问题描述】:

来自spring boot documentation@ConfigurationProperties

从带注释的项目生成您自己的配置元数据文件 使用@ConfigurationProperties

我尝试在我的配置类上分别使用@Configuration@ConfigurationProperties

@Component
//@Configuration
@ConfigurationProperties 
@EnableSpringDataWebSupport
@EnableAsync
public class AppConfig {
    ...
}

我没有看到任何明显的差异。

@ConfigurationProperties@Configuration有什么用?

【问题讨论】:

  • 您阅读过@Configuration 的文档吗?很明显,他们做的事情完全不同。

标签: spring-boot


【解决方案1】:

@Configuration 用于创建创建新 bean 的类(通过使用 @Bean 注释其方法):

@Configuration
public class CustomConfiguration {

    @Bean
    public SomeClass someClass() {
        return new SomeClass();
    }
}

@ConfigurationProperties 将外部配置绑定到它注释的类的字段中。通常将它与@Bean 方法一起使用来创建一个封装了可以外部控制的配置的新bean。

这是我们如何使用它的真实示例。考虑一个简单的 POJO,它包含一些与连接到 ZooKeeper 相关的值:

public class ZookeeperProperties
{
    private String connectUrl;

    private int sessionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis(5);

    private int connectTimeoutMillis = (int) TimeUnit.SECONDS.toMillis(15);

    private int retryMillis = (int) TimeUnit.SECONDS.toMillis(5);

    private int maxRetries = Integer.MAX_VALUE;

    // getters and setters for the private fields
}

现在我们可以创建一个 ZookeeperProperties 类型的 bean 并使用外部配置自动填充它:

@Configuration
public class ZooKeeperConfiguration {

    @ConfigurationProperties(prefix = "zookeeper")
    @Bean
    public ZookeeperProperties zookeeperProperties() {

        // Now the object we create below will have its fields populated
        // with any external config that starts with "zookeeper" and
        // whose suffix matches a field name in the class.
        //
        // For example, we can set zookeeper.retryMillis=10000 in our
        // config files, environment, etc. to set the corresponding field
        return new ZookeeperProperties();
    }
}

这样做的好处是它比在ZookeeperProperties 的每个字段中添加@Value 更简洁。相反,您在 @Bean 方法上提供单个注释,Spring 会自动将它找到的具有匹配前缀的任何外部配置绑定到该类的字段。

它还允许我的班级的不同用户(即创建ZookeeperProperties bean 类型的任何人)使用他们自己的前缀来配置班级。

【讨论】:

  • 能否给我一个ConfigurationProperties的使用案例,如果我想在外部设置一些值,我可以直接从aplication.properties yaml文件中获取值。
  • 当然,我在答案中添加了一个新示例。
  • 我相信这个设置还有一个捷径;即用@Component@ConfigurationProperties(prefix="zookeeper") 注释ZookeeperProperties。这将使 ZookeeperProperties 可以自动装配。当然,这仅适用于在ZookeeperConfiguration 中没有其他@Beans 的情况。
  • 以这种方式定义的另一个优点是您可以将配置和相应的属性定义保存在一个地方
【解决方案2】:

ConfigurationProperties 的用例是用于外部化配置。

@Configuration

  • 表示一个类声明了一个或多个@Bean 方法,并且可以由 Spring 容器处理以在运行时为这些 bean 生成 bean 定义和服务请求。

@ConfigrationProperties

-- 如果要绑定和验证某些外部属性(例如,来自 .properties 文件),则添加到类定义或 @Configuration 类中的 @Bean 方法中。

查看屏幕截图以区分 @Value@ConfigurationProperties

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties

【讨论】:

    猜你喜欢
    • 2020-07-12
    • 2015-12-31
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2017-02-19
    • 2018-02-07
    相关资源
    最近更新 更多