【Spring Boot基础】@ConfigurationProperties的使用

一、代码展示

1、新建Person类,代码如下:

package com.lc.springboot_testbilbil;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@ConfigurationProperties
public class Person {
    private String name;//name和age要与yml里的一致!
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2、Controller类代码如下:

package com.lc.springboot_testbilbil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${name}")
    private String Name;

    @Value("${person1.name}")
    private String Name1;

    @Autowired
    private Environment env;

    @Autowired
    private Person person3;

    @RequestMapping("/helloYML")
    public String hello(){
        System.out.println(Name);
        System.out.println(Name1);
        System.out.println("person2.name是"+env.getProperty("person2.name"));
        System.out.println("person2.age是"+env.getProperty("person2.age"));
        System.out.println("person1.name是"+env.getProperty("person1.name"));
        System.out.println("person1.age是"+env.getProperty("person1.age"));
        System.out.println("-----------------------------");
        System.out.println(person3);
        return "Hello World! 你好林月明!";
    }
}

3、Application类代码如下:

package com.lc.springboot_testbilbil;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootTestBilBilApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTestBilBilApplication.class, args);
    }

}

4、.yml文件中代码如下:

 server:
   port: 8081

 name : XiaoLi
#对象
 person1:
   name : ${name}
#XiaoLi
   age : 20

#对象行内写法
 person2 : {name: XiaoYue, age: 30}

#数组
 address1 :
   -Nanning
   -Liuzhou

  #数组行内写法
 address2 : [Nanning,Liuzhou]

#纯量
 msg1 : 'hello \n world'
#不会识别转义字符,导致原样输出
 msg2 : "hello \n world"
#会识别转义字符

5、参考链接2,注意该文件中至少存在一对花括号,否则可能会报错Error:java: Invalid additional meta-data in 'META-INF/spring-configuration-metadata.json': End of in...

{
}

6、后台运行结果:

 二、若Person类中@ConfigurationProperties带有前缀prefix

@ConfigurationProperties(prefix = "person2")

1、运行结果

 2、原因解释

带有前缀prefix= "person3",才会得到对应person3配置中的值。若无前缀,int默认值为0。

name : XiaoLi
#对象
 person1:
   name : ${name}
#XiaoLi
   age : 20

#对象行内写法
 person2 : {name: XiaoYue, age: 30}

三、参考教程

1、黑马程序员SpringBoot基础教程https://www.bilibili.com/video/BV1Lq4y1J77x?p=11&vd_source=841fee104972680a6cac4dbdbf144b50

2、resources报错解决https://blog.csdn.net/niuzaiwenjie/article/details/80882934

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot的@ConfigurationProperties注解用于将配置文件中的属性Java类中的属性进行绑定。如果在使用Spring Boot 2.4.5版本时@ConfigurationProperties注解不生效,可能是由于以下几个原因: 1. 未添加@Configuration注解:在使用@ConfigurationProperties注解的类上要添加@Configuration注解,以确保该类被Spring容器扫描并创建Bean。 2. 未在Spring Boot的入口类上添加@EnableConfigurationProperties注解:如果@ConfigurationProperties注解的类不是在Spring Boot的入口类中被创建的Bean,需要在入口类上添加@EnableConfigurationProperties注解来启用@ConfigurationProperties注解的生效。 3. 配置文件中的前缀不正确:@ConfigurationProperties注解中的prefix属性用于指定配置文件中的属性前缀,要确保该前缀与配置文件中的属性前缀一致。 4. 未在配置文件中添加对应的属性:如果使用@ConfigurationProperties注解将配置文件中的属性Java类中的属性进行绑定,要确保配置文件中存在对应的属性。 5. 未使用setter方法:在使用@ConfigurationProperties注解绑定属性时,需要提供相应的setter方法,以便Spring Boot能够将属性值设置到Java类中的属性中。 如果以上原因都排除了还是无法生效,可以尝试重新编译并清理项目,或者考虑升级到最新版本的Spring Boot。另外,也可以在Spring Boot应用启动时打印日志,查看@ConfigurationProperties相关的日志输出,以便进一步排查问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林月明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值