ChatGPT:@EnableConfigurationProperties 和 @ConfigurationProperties注解

ChatGPT:@EnableConfigurationProperties 和 @ConfigurationProperties注解



@EnableConfigurationProperties(ServiceProperties.class) @ConfigurationProperties(prefix = “cestc”) 这两个注解的关系

@EnableConfigurationProperties@ConfigurationProperties 是 Spring Boot 中用于外部化配置的两个注解,它们通常一起使用来简化配置的管理。

@ConfigurationProperties

@ConfigurationProperties 注解用于将外部配置(例如,application.propertiesapplication.yml 文件中的配置)绑定到一个 Java Bean 上。这个注解通常会加在一个类上,并通过指定一个前缀来绑定相关的配置属性。

例如:

@ConfigurationProperties(prefix = "cestc")
public class ServiceProperties {
    private String url;
    private int timeout;
    
    // getters and setters
}

在上面的例子中,ServiceProperties 类会将配置文件中以 cestc 开头的属性(例如,cestc.urlcestc.timeout)映射到类的属性中。

@EnableConfigurationProperties

@EnableConfigurationProperties 注解通常加在一个配置类上,用于启用对使用 @ConfigurationProperties 注解的 bean 的支持。它可以在任何 @Configuration 类中使用,以确保 @ConfigurationProperties 注解的类被 Spring Boot 扫描和注册为 Spring 容器中的 bean。

例如:

@Configuration
@EnableConfigurationProperties(ServiceProperties.class)
public class AppConfig {
    // other configuration
}

在这个例子中,AppConfig 类通过 @EnableConfigurationProperties 注解启用了 ServiceProperties 配置类的支持,这样 Spring Boot 会自动扫描和加载 ServiceProperties 中的配置。

总结
  1. @ConfigurationProperties:用于定义一个 Java Bean,该 Bean 的属性会从外部配置文件中绑定。
  2. @EnableConfigurationProperties:用于启用和注册 @ConfigurationProperties 注解的配置类,使其成为 Spring 容器中的一个 bean。

它们的关系是:@EnableConfigurationProperties 启用 @ConfigurationProperties,使得 Spring Boot 能够扫描并将配置属性绑定到指定的 Java Bean 上。


@ConfigurationProperties 实现这个注解的类,需要有get set方法吗

是的,实现 @ConfigurationProperties 注解的类通常需要有 getter 和 setter 方法。这些方法用于在 Spring Boot 启动时将配置文件中的属性值绑定到该类的字段上。

示例

假设你的配置文件(例如 application.yml)中有如下内容:

cestc:
  url: http://example.com
  timeout: 5000

你可以创建一个类,并使用 @ConfigurationProperties 注解来绑定这些属性:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "cestc")
public class ServiceProperties {

    private String url;
    private int timeout;

    // Getter and Setter for url
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    // Getter and Setter for timeout
    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
}
作用
  1. Getter 方法:Spring Boot 使用 getter 方法读取配置文件中的属性值。
  2. Setter 方法:Spring Boot 使用 setter 方法将配置文件中的属性值设置到对应的字段上。
例外情况

如果你使用的是 Lombok 库,你可以通过 Lombok 注解自动生成这些 getter 和 setter 方法,从而简化代码。例如:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;

@Component
@ConfigurationProperties(prefix = "cestc")
@Data
public class ServiceProperties {
    private String url;
    private int timeout;
}

@Data 注解会自动生成所有字段的 getter 和 setter 方法,这样就不需要手动编写这些方法了。

总结

实现 @ConfigurationProperties 注解的类需要有 getter 和 setter 方法,以便 Spring Boot 能够将外部配置文件中的属性值正确地绑定到该类的字段上。如果使用 Lombok,可以通过注解自动生成这些方法,从而简化代码。


  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@EnabaleConfigurationProperties和@ConfigurationPropertiesSpring Boot中用于处理配置文件的注解。 @EnableConfigurationProperties用于启用@ConfigurationProperties注解,并将配置文件中的值与对应的属性进行绑定。它需要配合@Configuration一起使用,通过将@ConfigurationProperties注解的配置类作为参数传递给@EnableConfigurationProperties来实现。 @ConfigurationProperties注解用于指定配置类中属性所对应的前缀,并将配置文件中的值与属性进行绑定。当一个配置类只被@ConfigurationProperties标记时,配置项的值不会被绑定到属性上,也不会将其注册为Bean。需要同时使用@Component或其子类注解(例如@Configuration)来将配置类注册为一个Bean。 举个例子,如果有一个配置类叫做MyConfig,使用@EnableConfigurationProperties将其启用,并使用@ConfigurationProperties注解指定前缀为"my.config",那么在配置文件中以"my.config"为前缀的配置项的值会被绑定到MyConfig类中对应的属性上。 ``` @Configuration @EnableConfigurationProperties(MyConfig.class) public class AppConfig { } @ConfigurationProperties(prefix = "my.config") public class MyConfig { private String property1; private int property2; // 其他属性和getter/setter方法 } ``` 在上述例子中,配置文件中的"my.config.property1"和"my.config.property2"的值会被绑定到MyConfig类的property1和property2属性上。 总结起来,@EnabaleConfigurationProperties用于启用@ConfigurationProperties注解,而@ConfigurationProperties用于指定配置类中属性的前缀,并将配置文件中的值与属性进行绑定。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [@EnableConfigurationProperties和@ConfigurationProperties用法及注意事项](https://blog.csdn.net/yuec1998/article/details/126357003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [@EnableConfigurationProperties @ConfigurationProperties @ConfigurationPropertiesScan](https://blog.csdn.net/u013202238/article/details/107133200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值