SpringBoot配置参数绑定@ConfigurationProperties@Value

本文详细介绍了如何在Spring Boot中将配置文件的参数绑定到组件属性上,包括使用@Value单个属性绑定和@ConfigurationProperties批量属性绑定。通过示例展示了@Component、@Repository、@Service、@Controller等不同组件的注册方式,并对比了@Configuration+@Bean与@EnableConfigurationProperties+@ConfigurationProperties的使用场景,强调了后者虽然可用但不推荐的做法。最后提供了完整代码示例及运行结果,帮助读者理解参数绑定的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作用

  • 将spring配置文件中配置的参数绑定给组件的属性

实现

步骤

  1. 容器中注册组件
  2. 参数绑定

注册组件

  • @Component
  • @Repository
  • @Service
  • @Controller
  • @RestController
  • @Configuration+@Bean
  • 等等

参数绑定种类

  • 单个属性绑定:组件+@Value
  • 批量属性绑定:组件+@ConfigurationProperties
  • 批量属性绑定:配置类@EnableConfigurationProperties+@ConfigurationProperties

说明:容器组件@EnableConfigurationProperties+@ConfigurationProperties也是可以实现配置参数绑定功能,但是从系统设计角度不推荐这么做;就像@Component+@Bean也能实现组件注册功能一样能实现但是不推荐

@Value和@ConfigurationProperties示例

  • 待绑定属性的容器组件
package com.ultra.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "user1")
public class User {
    private String name;
    @Value("use1.nickname")
    private String nickname;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
  • spring配置文件
user1.name=XiaoMing
user1.nickname=XiaoBaiLong
  • 测试类/启动类
package com.ultra;

import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author admin
 */
@SpringBootApplication
public class SpringbootWebApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);
        User user = applicationContext.getBean(User.class);
        System.out.println(user);
    }
}
  • 结果
    在这里插入图片描述

配置类@EnableConfigurationProperties+@ConfigurationProperties示例

  • 待绑定属性的容器组件
package com.ultra.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "user1")
public class User {
    private String name;
    @Value("use1.nickname")
    private String nickname;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
  • spring配置文件
user1.name=XiaoMing
user1.nickname=XiaoBaiLong
  • 配置类
package com.ultra.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({User.class})
public class UserConfig {
}
  • 测试类/启动类
package com.ultra;

import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author admin
 */
@SpringBootApplication
public class SpringbootWebApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);
        User user = applicationContext.getBean(User.class);
        System.out.println(user);
    }
}
  • 结果
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

那你为何对我三笑留情

感谢支持,沉淀自己,帮助他人!

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

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

打赏作者

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

抵扣说明:

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

余额充值