欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > springboot中@ConfigurationProperties注解的用法

springboot中@ConfigurationProperties注解的用法

2024/10/24 1:57:24 来源:https://blog.csdn.net/GDFHGFHGFH/article/details/142781226  浏览:    关键词:springboot中@ConfigurationProperties注解的用法

简介

@ConfigurationProperties注解在Spring Boot框架中提供了一种将外部配置(如application.properties或application.yml文件中的属性)绑定到Java对象的便捷方式。以下是@ConfigurationProperties注解的详细用法

基本用法

定义配置类

  • 创建一个普通的Java类,并使用@ConfigurationProperties注解来指定配置文件中属性的前缀。
  • 类中的字段需要与配置文件中的属性名称相匹配(考虑驼峰命名和下划线的转换)。
@Component  
@ConfigurationProperties(prefix = "test")  
public class ConfigProperties {  private String hostName;  private int port;  private String from;  // 标准的getter和setter方法  
}

在这个例子中,@ConfigurationProperties注解的prefix属性指定了配置文件中属性的前缀为"test",因此Spring会查找以"test"开头的属性,并将它们映射到ConfigProperties类的相应字段上。这里配置类上如果没有使用@Component等注解,则需要在主类上使用@EnableConfigurationProperties注解来绑定属性到POJO中。,如果使用了@Component等注解,那就不需要在主类上使用@EnableConfigurationProperties注解来绑定属性到POJO中,直接使用 @Autowired这类注解直接注入就好

在配置文件中定义属性

  • 在application.properties或application.yml文件中定义与配置类字段相匹配的属性。
# application.properties  
test.hostname=mailer@mail.com  
test.port=9000  
test.from=mailer@mail.com

使用配置类

  • 配置类可以通过依赖注入的方式在应用程序的其他部分中使用。
@RestController  
@RequestMapping(value = "/config")  
public class ConfigurationPropertiesController {  @Autowired  private ConfigProperties configProperties;  @GetMapping("/test")  public Map<String, Object> test() {  Map<String, Object> map = new HashMap<>();  map.put("hostName", configProperties.getHostName());  map.put("port", configProperties.getPort());  map.put("from", configProperties.getFrom());  return map;  }  
}

高级用法

与@ConfigurationPropertiesScan注解一起使用:
  • 从Spring Boot 2.2开始,Spring通过类路径扫描查找并注册@ConfigurationProperties类。因此,在某些情况下,不需要使用@Component注解来注释此类类,甚至不需要使用@EnableConfigurationProperties注解。
  • 可以使用@ConfigurationPropertiesScan注解来扫描配置属性类的自定义位置。
@SpringBootApplication  
@ConfigurationPropertiesScan("com.xxx.configurationproperties")  
public class EnableConfigurationDemoApplication {  public static void main(String[] args) {  SpringApplication.run(EnableConfigurationDemoApplication.class, args);  }  
}

嵌套属性

  • @ConfigurationProperties注解支持嵌套属性,可以在配置类中定义List、Map和类作为字段。
@Configuration  
@ConfigurationProperties(prefix = "test")  
public class ConfigProperties {  private String host;  private int port;  private String from;  private List<String> defaultRecipients;  private Map<String, String> additionalHeaders;  private Credentials credentials;  // 标准的getter和setter方法  
}  public class Credentials {  private String authMethod;  private String username;  private String password;  // 标准的getter和setter方法  
}

配置文件内容需要相应地设置所有字段:

# Simple properties  
test.hostname=mailer@mail.com  
test.port=9000  
test.from=mailer@mail.com  
# List properties  
test.defaultRecipients[0]=admin@mail.com  
test.defaultRecipients[1]=owner@mail.com  
# Map Properties  
test.additionalHeaders.redelivery=true  
test.additionalHeaders.secure=true  
# Object properties  
test.credentials.username=john  
test.credentials.password=password  
test.credentials.authMethod=SHA1

使用@ConfigurationProperties作用在@Bean注释的方法上

  • 这种方式非常适合绑定到无法控制的第三方组件上。
@Configuration  
public class ConfigProperties {  @Bean  @ConfigurationProperties(prefix = "item")  public Item item() {  return new Item();  }  
}

注意事项

属性名称的一致性:
  • 字段名称和属性名称之间需要保持一致性,考虑下划线和驼峰命名的转换。
配置类的注册:
  • 如果配置类上没有使用@Component等注解,则需要在主类上使用@EnableConfigurationProperties注解来绑定属性到POJO中。
动态更新:
  • @ConfigurationProperties支持配置的动态更新,但在某些情况下,频繁的配置更新可能会带来性能开销。

综上所述,@ConfigurationProperties注解在Spring Boot中提供了一种灵活且动态的方式来管理配置属性,通过简单的注解和配置文件即可实现配置的绑定和更新。

版权声明:

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

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