【Spring】@ConfigurationProperties 注解的简单使用和介绍


本篇文章主要介绍SringBoot中的@ConfigurationProperties注解,该注解是用来获取yml或者properties配置文件的配置信息,下面根据一些配置信息给出案例代码进行讲解。


@ConfigurationProperties 中属性介绍

属性名称介绍
prefix、value指定属性前缀
ignoreInvalidFields是否忽略无效字段,如果设置为true,则在绑定过程中遇到无效字段不会抛出异常
下面有对该属性值的专门介绍
默认是false
ignoreUnknownFields是否忽略未知字段,如果设置为true,则在绑定过程中遇到未知字段不会抛出异常
下面有对该属性值的专门介绍
默认是true


案例代码介绍

下面在yml配置文件中配置:

user:
  name: "张三"
  age: 18
  hobbies: 
    - "看书"
    - "写代码"

代码实现:

@Component
@ConfigurationProperties(prefix = "user")
public class UserInfo {
    private String name;
    private int age;
    private List<String> hobbies;
}

这样,就能从配置文件中读取到配置的信息。



ignoreInvalidFields 属性用法

如果,我把ignoreInvalidFields属性值设置为falseignoreInvalidFields属性值默认值就是fasle),然后把yml配置文件中的age修改成字符串类型,这样配置文件中的类型和Class中的类型不一致就会出现错误,以下是代码案例

user:
  name: "张三"
  age: 18
  hobbies: 
    - "看书"
    - "写代码"

代码实现:

@Component
@ConfigurationProperties(prefix = "user", ignoreInvalidFields = false)
public class UserInfo {
    private String name;
    private int age;
    private List<String> hobbies;
}

错误如下:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'user.age' to int:

    Property: user.age
    Value: 18岁
    Origin: class path resource [application.yml] - 10:8
    Reason: failed to convert java.lang.String to int (caused by java.lang.NumberFormatException: For input string: "18岁")

Action:

Update your application's configuration


ignoreUnknownFields 属性用法

这里,我把ignoreUnknownFields属性值设置为falseignoreUnknownFields属性值默认值是true),然后把UserInfo文件中的hobbies变量删除,这样配置文件中的类型和Class中的属性就会不一致,接下来就会出现错误,以下是代码案例

user:
  name: "张三"
  age: 18
  hobbies: 
    - "看书"
    - "写代码"

代码实现:

@Component
@ConfigurationProperties(prefix = "user", ignoreUnknownFields = false)
public class UserInfo {
    private String name;
    private int age;
}

错误如下:

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@31e2232f type = com.demo.UserInfo, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=false, ignoreUnknownFields=false, prefix=user, value=user)]] failed:

    Property: user.hobbies[0]
    Value: 看书
    Origin: class path resource [application.yml] - 12:7
    Reason: The elements [user.hobbies[0],user.hobbies[1]] were left unbound.
    Property: user.hobbies[1]
    Value: 写代码
    Origin: class path resource [application.yml] - 13:7
    Reason: The elements [user.hobbies[0],user.hobbies[1]] were left unbound.

Action:

Update your application's configuration





End


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用@ConfigurationProperties注解来将配置文件中的属性值与Java对象进行绑定。这样可以方便地管理应用程序的配置属性。 下面是使用@ConfigurationProperties注解的步骤: 1. 创建一个Java类,并在类上添加@ConfigurationProperties注解。该注解的value属性指定了配置文件中的前缀,prefix属性指定了配置文件中的属性前缀。例如,如果配置文件中的属性为myconfig.name,那么可以将value属性设置为"myconfig"。 ```java @ConfigurationProperties(prefix = "myconfig") public class MyConfigProperties { private String name; // getter和setter方法 } ``` 2. 在配置文件(application.properties或application.yml)中定义属性值。例如,在application.properties中添加以下内容: ``` myconfig.name=bj ``` 3. 在Spring Boot应用程序的主类中,使用@EnableConfigurationProperties注解来启用@ConfigurationProperties注解。将@ConfigurationProperties注解的类作为参数传递给@EnableConfigurationProperties注解。 ```java @SpringBootApplication @EnableConfigurationProperties(MyConfigProperties.class) public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 现在,可以在其他组件中注入使用@ConfigurationProperties注解的类,并使用其中的属性值。例如,在一个Service类中注入MyConfigProperties类: ```java @Service public class MyService { private final MyConfigProperties configProperties; public MyService(MyConfigProperties configProperties) { this.configProperties = configProperties; } public void printName() { System.out.println("Name: " + configProperties.getName()); } } ``` 这样,就可以在应用程序中使用@ConfigurationProperties注解来管理配置属性了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值