千家信息网

SpringBoot中@ConfigurationProperties注解怎么实现配置绑定

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容介绍了"SpringBoot中@ConfigurationProperties注解怎么实现配置绑定"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下
千家信息网最后更新 2026年11月01日SpringBoot中@ConfigurationProperties注解怎么实现配置绑定

本篇内容介绍了"SpringBoot中@ConfigurationProperties注解怎么实现配置绑定"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

properties配置文件如下:

human.name=Mr.Yuhuman.age=21human.gender=male

如何把properties里面的配置绑定到JavaBean里面,以前我们的做法如下:

public class PropertiesUtil {    public static void getProperties(Person person) throws IOException {        Properties properties = new Properties();        properties.load(new FileInputStream("src/main/resources/demo.properties"));        //得到配置文件key的名字        Enumeration enumeration = properties.propertyNames();        while (enumeration.hasMoreElements()){            String key =(String)enumeration.nextElement();            String value = properties.getProperty(key);            System.out.println("key="+key+"------value="+value);            //封装到JavaBean            //以下内容省略        }    }}

输出结果:

key=human.name——————value=Mr.Yu
key=human.age——————value=21
key=human.gender——————value=male

Process finished with exit code 0

可以看到这个过程十分繁杂,但是在SpringBoot中这个过程将会变得非常简单。

在SpringBoot中有如下3种方法:

1.@ConfigurationProperties注解+@Component(或@Controller或@Service或@Repository)注解

  • 只有在容器中的组件,才会拥有SpringBoot提供的强大功能,也就是如果我们需要使用到@ConfigurationProperties注解,那么我们首先要保证该对JavaBean对象在IoC容器中,所以需要用到Component注解来添加组件到容器中。

  • @ConfigurationProperties注解中prefix与value互相都是别名

  • 也就是说@ConfigurationProperties(value = "human")与@ConfigurationProperties(prefix = "human")是一样的

  • prefix和value指的是前缀的意思

代码测试: properties配置文件:

human.name=Mr.Yuhuman.age=21human.gender=male

Person类:

@Component ////只有在容器中的组件,才会拥有SpringBoot提供的强大功能@ConfigurationProperties(value = "human")public class Person {    public String name;    public int age;    public String gender;    public Person() {    }    public Person(String name, int age, String gender) {        this.name = name;        this.age = age;        this.gender = gender;    }    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;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                ", age=" + age +                ", gender='" + gender + '\'' +                '}';    }}

测试类如下:

//@Controller////@ResponseBody以字符串的方式写给浏览器,而不是跳转到某个页面//@ResponseBody//@RestController =  @Controller +  @ResponseBody@RestControllerpublic class HelloController {    //自动注入属性    @Autowired    Person person;    @RequestMapping("/person")    public Person getPerson(){        return person;    }}

测试结果:

@ConfigurationProperties注解+@EnableConfigurationProperties注解

这种方式@EnableConfigurationProperties注解一定要在配置类上写,@EnableConfigurationProperties的意思是开启属性配置功能,开启谁的属性配置功能呢,因为我们上面的Person类想进行配置绑定,所以我们在后面加上参数Person.class:@EnableConfigurationProperties(Person.class)

@EnableConfigurationProperties(Person.class)的作用就是把这个Person组件注册到容器中,对象的名称为:human-com.ysw.boot.properties.Person 其中human是@ConfigurationProperties(value = "human")中前缀value的值

在Person类上还是有@ConfigurationProperties注解,这种方式把Person类上的@Component注解换成了配置类上的@EnableConfigurationProperties(Person.class)注解

这种方式主要用在引用第三方包时,比如第三方包中有一个Person类,该类中没有使用@Component,我们也不能够给第三方包中的类加上@Component,这个时候就可以使用在配置类上加上@EnableConfigurationProperties注解的方法。

@ConfigurationProperties注解+@Import注解

  • 使用@Import给容器中自动创建出这个类型的组件、默认组件的名字就是全类名

  • @Import注解与2中的@ConfigurationProperties注解效果是一样的

  • 只不过他们注册到容器中的名称不同:

    • @ConfigurationProperties注解注册到容器中对象的名称为:human-com.ysw.boot.properties.Person 其中human是@ConfigurationProperties(value = "human")中前缀value的值

    • @Import注解注册到容器中对象的名称为:com.ysw.boot.properties.Person

配置类:

Person类:

测试类:

application.properties文件:

server.port=8888human.name=Mr.Yuhuman.age=21human.gender=male222

测试结果:

"SpringBoot中@ConfigurationProperties注解怎么实现配置绑定"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

注解 配置 容器 组件 测试 功能 名称 对象 文件 方式 内容 前缀 属性 第三方 结果 过程 强大 也就是 只有 名字 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 南京欣网互联网络科技 科技股互联网 智能科技公司是互联网公司吗 互联网科技公司怎么样 互联网与新科技 北京互联网络科技有限公司 智慧科技互联网 互联网科技logo设计 甘肃拓世互联网科技有限公司 衡水科技产业互联网平台 搜狐科技互联网 如何创新互联网科技 企晟互联网信息科技有限公司 阿里互联网科技企业 玉溪新华互联网科技好不好 济南有互联网科技公司吗 杭州披云加互联网科技有限公司 互联网科技手机排行榜 互联网科技企业商标注意事项 武汉亿视互联网科技有限公司 湖北九亚互联网络科技有限公司 迪茉互联网科技 有木互联网科技 科技互联网创业计划书 镁科技互联网资讯平台 中国的互联网科技企业的崛起 银川新华互联网科技职业技术培训学校图片 互联网5g黑科技有哪些 玉溪新华互联网科技好不好 江苏互联网智能科技产品出厂价
0