spring boot 配置绑定

1、在pom.xml中添加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2、在application.properties文件中填加自己定义的配置信息,如

#自定义配置
self.user.name=ldy
self.user.age=27
self.user.phones[0]=139000000000
self.user.phones[1]=138000000000
self.user.phones[2]=135000000000
#上面的多个电话信息也可以用逗号分隔的形势
#self.user.phones=139000000000,138000000000,135000000000

3、读取配置信息

(1)获取单个配置信息

        可以通过@Value注解,绑定单个参数,如:

@Value(value = "${self.user.name}") 
private String name;

        通过以上方法获取单个参数的值,不用写get个set方法

(2)将配置信息注入到对应的实体类

        编写要绑定的实体:UserProperties.java实体类

package com.ldy.bootv2.demo.entity;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="self.user")
public class UserProperties {

    private String name;
    private int age;
    private List<String> phones;

    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 List<String> getPhones() {
	return phones;
    }

    public void setPhones(List<String> phones) {
	this.phones = phones;
    }
    
}

4、编写测试接口,获取自定义配置信息

package com.ldy.bootv2.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ldy.bootv2.demo.entity.UserProperties;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "测试接口-API")
public class HelloController {
	
	private static Logger logger = LoggerFactory.getLogger(HelloController.class);
	
	@Value(value = "${self.user.name}") 
	private String name;
	
	@Autowired
	private UserProperties userProperties;

	@GetMapping("/hello")
	@ApiOperation("hello的测试接口")
	@ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "String")
	public String index(@RequestParam(required = true) final String name) {
		logger.info("您调用了hello 接口");
		return "hello " + name;
	}

	@PostMapping("/sum")
	@ApiOperation("两整数求和接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "a", value = "参数a", required = true, dataType = "int"),
			@ApiImplicitParam(name = "b", value = "参数b", required = true, dataType = "int") })
	public String sum(@RequestParam(required = true) final Integer a, @RequestParam(required = true) final Integer b) {
		logger.info("您调用了sum 接口");
		int sum = a + b;
		return "a + b = " + sum;
	}
	
	@GetMapping("/getName")
	@ApiOperation("获取配置文件中的用户名称")
	public String getName() {
		return "hello " + name;
	}
	
	@GetMapping("/getUser")
	@ApiOperation("获取配置文件中的用户完整信息")
	public UserProperties getUser() {
		return userProperties;
	}

}

5、运行项目,通过swagger页面,调用测试接口,测试运行情况

    

     没有集成swagger的同学可以看我的上一篇博客:https://blog.csdn.net/LDY1016/article/details/83415640

(1)获取单个配置信息结果如图:

(2)获取用户全部配置信息结果如图:

源码下载地址:https://pan.baidu.com/s/1Z771VDiuabDBJJV445xLeA#list/path=%2Fspring%20boot%202.x%20%E4%BB%A3%E7%A0%81

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值