Spring Boot 如何获取配置文件信息

14 篇文章 0 订阅
12 篇文章 1 订阅

获取方式一:

如下图所示创建三个文件

platform-configration.properties代码如下:

# public address
adress=http://localhost:9980

# Push Settings
push.url=${adress}/common/push

# Short Message Settings
shortmessage.url=${adress}/common/shortMessage
publicmessage.url=${adress}/common/publicMessage
Configration代码如下:
@Component
@PropertySource("classpath:platform-configration.properties")
public class Configration {

    @Value("${push.url}")
    private String push;

    @Value("${shortmessage.url}")
    private String shortmessage;

    @Value("${publicmessage.url}")
    private String publicmessage;

    public String getPush() {
        return push;
    }

    public void setPush(String push) {
        this.push = push;
    }

    public String getShortmessage() {
        return shortmessage;
    }

    public void setShortmessage(String shortmessage) {
        this.shortmessage = shortmessage;
    }

    public String getPublicmessage() {
        return publicmessage;
    }

    public void setPublicmessage(String publicmessage) {
        this.publicmessage = publicmessage;
    }
}
ConfigControllerOne代码如下:
package com.example.api.config.controller;

import com.example.api.config.Configration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestOperations;

import javax.annotation.Resource;
import java.net.URI;


/**
 * Created by XieZhiXin on 2018/7/25.
 */

@RestController
@RequestMapping("/test")
public class ConfigControllerOne {


    private final static Logger log = LoggerFactory.getLogger(ConfigControllerOne.class);

    @Autowired
    private Configration configration;

//    @Autowired
//    private RestOperations restOperations;

    @RequestMapping("/config")
    public String testConfig() {

        String push=configration.getPush();
        //此处获取到配置文件中的地址可转换为uri进行相关操作
        //ResponseEntity<?> response = null;
        //URI uri =URI.create(configration.getShortmessage());
        //response=restOperations.postForEntity(URI,Object, Class < T >);
        String publicmessage=configration.getPublicmessage();
        String shortmessage=configration.getShortmessage();
        return push;
        // return uri.toString();

    }
}

获取方式二:

如下图所示创建三个文件

application.yml代码如下:

server:
  port: 8001

  # 配置地址
url:
    # 订单微服务的地址
    orderUrl: http://localhost:8002
    # 用户微服务的地址
    userUrl: http://localhost:8003
    # 购物车微服务的地址
    shoppingUrl: http://localhost:8004

服务器端口号也可设置为下面方式,此方式比较灵活

MicroServiceController文件代码如下:
package com.example.api.config.controller;

import com.example.api.config.MicroServiceConfig;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by XieZhiXin on 2018/7/26.
 */
@RestController
@RequestMapping("/test")
public class MicroServiceController {

    private static final Logger log = LoggerFactory.getLogger(MicroServiceController.class);

    @Value("${url.orderUrl}")
    private String orderUrl;

    @Autowired
    private MicroServiceConfig microServiceConfig;
//    @Resource
//    private MicroServiceConfig microServiceConfig;

    @RequestMapping("/configtwo")
    public String testConfig() {
        log.info("获取的订单服务地址为:{}", orderUrl);
        // 使用配置类来获取
        log.info("订单服务地址为:{}", microServiceConfig.getOrderUrl());
        log.info("用户服务地址为:{}", microServiceConfig.getUserUrl());
        log.info("购物车服务地址为:{}", microServiceConfig.getShoppingUrl());
        return "success";
    }
}
MicroServiceConfig文件代码如下:
package com.example.api.config;


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

/**
 * Created by XieZhiXin on 2018/7/26.
 */
@Component
@ConfigurationProperties(prefix = "url")
public class MicroServiceConfig {

    private String orderUrl;
    private String userUrl;
    private String shoppingUrl;

    public String getOrderUrl() {
        return orderUrl;
    }

    public void setOrderUrl(String orderUrl) {
        this.orderUrl = orderUrl;
    }

    public String getUserUrl() {
        return userUrl;
    }

    public void setUserUrl(String userUrl) {
        this.userUrl = userUrl;
    }

    public String getShoppingUrl() {
        return shoppingUrl;
    }

    public void setShoppingUrl(String shoppingUrl) {
        this.shoppingUrl = shoppingUrl;
    }
}

* 感谢您的阅读。如果感觉文章对您有用,麻烦您动动手指点个赞,以资鼓励。谢谢!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值