概述
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
最后
以上就是淡淡鸡为你收集整理的spring boot 配置绑定的全部内容,希望文章能够帮你解决spring boot 配置绑定所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复