欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Spring Boot,注解,@ConfigurationProperties

Spring Boot,注解,@ConfigurationProperties

2025/11/9 18:58:55 来源:https://blog.csdn.net/Yqha1/article/details/148369565  浏览:    关键词:Spring Boot,注解,@ConfigurationProperties

好的,这是上面关于 @ConfigurationProperties 注解和 setter 方法的判断题及其解析的中文版本:


该判断题表述为:“使用@ConfigurationProperties 注解注入属性值时,必须为对应的属性提供setter方法。”

这个说法是 正确的。


@ConfigurationProperties 与 Setter 方法 ⚙️

在 Spring Boot 中使用 @ConfigurationProperties 注解将外部配置属性绑定到一个 Java bean 时,通常情况下,必须为希望绑定的字段提供 setter 方法。Spring Boot 的数据绑定工具主要使用这些 setter 方法(或者在使用构造函数绑定时使用构造函数参数)来填充字段。


关键知识点 🔑

  • @ConfigurationProperties 的目的:此注解提供了一种便捷的方式,可以将层级的配置属性(来自 application.propertiesapplication.yml)映射到一个强类型的 Java 对象。
  • 绑定机制:Spring Boot 使用其数据绑定机制。当一个对象被 @ConfigurationProperties 注解并被启用(例如,通过 @EnableConfigurationProperties 或其本身是一个 @Component)时,Spring 将尝试将与指定 prefix 匹配的属性绑定到该对象的字段上。
  • Setter 方法的要求:对于标准的 Java Bean 属性绑定,公共的 setter 方法是主要的实现机制。Setter 方法的名称必须遵循标准的 Java Bean 约定(例如,对于字段 myProperty,setter 方法应该是 setMyProperty(String myProperty))。
  • Lombok:如果你正在使用 Lombok,在你的配置属性类上添加 @Data@Setter 注解会自动生成所需的 setter 方法,从而减少样板代码。
  • 构造函数绑定 (Constructor Binding):作为 setter 方法的替代方案,尤其适用于不可变的配置对象,Spring Boot 支持构造函数绑定。你可以通过以下方式启用:
    • 在类上添加 @ConstructorBinding(在旧版 Spring Boot 中,如果存在多个构造函数或想明确指定时是必需的;在新版本中,如果只有一个参数化的构造函数,它通常会自动用于绑定配置属性)。
    • 提供一个构造函数,其参数名称与属性名称匹配(经过宽松绑定规范化后)。
    • 如果希望实现不可变性,可以将字段声明为 final
    • 在这种情况下,通过构造函数注入的字段不需要 setter 方法。
  • 宽松绑定 (Relaxed Binding):Spring Boot 使用“宽松绑定”规则,这意味着配置文件中的属性名称不必与 Java 类中的字段名或 setter 方法名完全一致。例如,YAML 中的 my-property-name 可以映射到 Java 中的 myPropertyName
  • 启用 @ConfigurationProperties
    • 在一个 @Configuration 类上使用 @EnableConfigurationProperties(YourPropertiesClass.class)
    • 或者,如果属性类本身就是一个 Spring bean(例如,用 @Component 注解),它将被自动处理。

实际案例 (使用 Setter 方法)

假设你在 application.yml 中有以下配置:

app:info:name: 我的超赞应用version: 1.0.2server-url: https://api.example.com

你的 Java 配置属性类将如下所示:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; // 或者使用 @EnableConfigurationProperties@Component // 使其成为 Spring bean 并有资格进行 @ConfigurationProperties 处理
@ConfigurationProperties(prefix = "app.info")
public class AppInfoProperties {private String name;private String version;private String serverUrl; // 字段名经过宽松绑定后与 server-url 匹配// 'name' 的 Setter 方法public void setName(String name) {this.name = name;}// 'version' 的 Setter 方法public void setVersion(String version) {this.version = version;}// 'serverUrl' 的 Setter 方法public void setServerUrl(String serverUrl) {this.serverUrl = serverUrl;}// 可选: Getter 方法以访问属性public String getName() {return name;}public String getVersion() {return version;}public String getServerUrl() {return serverUrl;}@Overridepublic String toString() {return "AppInfoProperties{" +"name='" + name + '\'' +", version='" + version + '\'' +", serverUrl='" + serverUrl + '\'' +'}';}
}

在其他组件中的使用:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.PostConstruct;@Service
public class AppService {private final AppInfoProperties appInfoProperties;@Autowiredpublic AppService(AppInfoProperties appInfoProperties) {this.appInfoProperties = appInfoProperties;}@PostConstructpublic void init() {System.out.println("应用名称: " + appInfoProperties.getName());System.out.println("应用版本: " + appInfoProperties.getVersion());System.out.println("服务器 URL: " + appInfoProperties.getServerUrl());System.out.println(appInfoProperties.toString());}
}

如果你从 AppInfoProperties 类中移除 setServerUrl 方法,那么 serverUrl 字段将不会从配置文件中填充(它会保持为 null),除非你切换到构造函数绑定。这证明了对于基于字段的、使用 @ConfigurationProperties 的属性注入,setter 方法的必要性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词