spring boot怎么获取配置文件的属性

这篇文章主要介绍“spring boot怎么获取配置文件的属性”,在日常操作中,相信很多人在spring boot怎么获取配置文件的属性问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”spring boot怎么获取配置文件的属性”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

server:
  port: 8888
  tomcat:
    uri-encoding: UTF-8
# 配置微服务的地址
url:
  # 订单微服务的地址
  orderUrl: http://localhost:8002
  #微服务地址2
  taskUrl: http://localhost:8003
  #微服务地址3
  customerUrl: http://localhost:8004
 
  那么我们如何获取呢?
第一种方式:直接使用@Value("${name}")注解就可以将配置文件中的属性值注入进来。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * 描述:微服务地址调用
 * @author Administrator
 * @create 2018-10-18 16:11
 */
@RestController
@RequestMapping("/url")
public class ConfigController {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);
 
    //在属性上使用 @Value 注解即可获取到配置文件中的配置信息
    @Value("${url.orderUrl}")
    private String orderUrl;
 
    @RequestMapping("/orderUrl")
    public String testConfig() {
        LOGGER.info("=====获取的订单服务地址为:{}", orderUrl);
        return orderUrl;
    }
}
第二种方式:多个配置信息的情形,列入我们有多个微服务地址,这样的话我们就还可以简单一些。
1 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
2 定义一个保存服务url的类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * 描述:微服务地址
 * @author Administrator
 * @create 2018-10-18 16:28
 */
@Component
@ConfigurationProperties(prefix = "url")
public class ServiceUrl {
    private String orderUrl;
    private String taskUrl;
    private String customerUrl;
    public String getOrderUrl() {
        return orderUrl;
    }
    public void setOrderUrl(String orderUrl) {
        this.orderUrl = orderUrl;
    }
 
    public String getTaskUrl() {
        return taskUrl;
    }
 
    public void setTaskUrl(String taskUrl) {
        this.taskUrl = taskUrl;
    }
 
    public String getCustomerUrl() {
        return customerUrl;
    }
    使用 @ConfigurationProperties 注解并使用 prefix 指定一个前缀,那么该类中的属性名就是配置中去掉前缀后的名字,一一对应即可。即:前缀名 + 属性名就是配置文件中定义的 key。同时,该类上面需要加上 @Component 注解,把该类作为组件放到 Spring 容器中,让 Spring 去管理,我们使用的时候直接注入即可。
然后我们直接使用@Resource注入就可以使用了
import com.ruifeng.demo.common.ServiceUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
 
/**
 * 描述:微服务地址调用
 * @author Administrator
 * @create 2018-10-18 16:11
 */
@RestController
@RequestMapping("/url")
public class ConfigController {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);
 
 
    @Resource
    private ServiceUrl microServiceUrl;
 
    @RequestMapping("/config")
    public String testConfigs() {
        LOGGER.info("=====获取的订单服务地址为:{}", microServiceUrl.getOrderUrl());
        LOGGER.info("=====获取的任务服务地址为:{}", microServiceUrl.getTaskUrl());
        LOGGER.info("=====获取的客户服务地址为:{}", microServiceUrl.getCustomerUrl());
 
        return "success";
    }
}

到此,关于“spring boot怎么获取配置文件的属性”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!