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.1 引入依赖

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

2.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";
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取spring-boot-admin-starter-client自定义的属性,可以按照以下步骤进行操作: 1. 首先,确保你的项目中已经添加了spring-boot-admin-starter-client包,并成功进行了打包。 2. 在项目的配置文件(比如application.properties或application.yml)中,可以使用以下格式设置spring-boot-admin-starter-client的自定义属性: ``` spring.boot.admin.client.custom-property=value ``` 其中,custom-property是你自定义的属性名,value是对应的属性值。 3. 可以通过在spring-boot-admin-starter-server项目的代码中使用@ConfigurationProperties注解来获取spring-boot-admin-starter-client的自定义属性。例如: ```java @ConfigurationProperties(prefix = "spring.boot.admin.client") public class MyConfiguration { private String customProperty; // getter and setter // other configurations } ``` 在这个配置类中,使用prefix属性指定了属性的前缀为"spring.boot.admin.client",这样就能获取spring-boot-admin-starter-client的自定义属性。 4. 运行spring-boot-admin-starter-server项目时,就可以通过MyConfiguration类获取spring-boot-admin-starter-client的自定义属性了。 综上所述,你可以通过在项目的配置文件中设置属性,并使用@ConfigurationProperties注解获取这些自定义属性获取spring-boot-admin-starter-client的自定义属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring-boot-admin-client-2.6.2-API文档-中文版.zip](https://download.csdn.net/download/qq_36462452/85294581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [spring-boot-admin-starter-client与spring-boot版本不匹配的坑](https://blog.csdn.net/mahui_1980/article/details/117528352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值