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

获取方式一:

如下图所示创建三个文件

Spring Boot 如何获取配置文件信息_第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();

    }
}

获取方式二:

如下图所示创建三个文件

Spring Boot 如何获取配置文件信息_第2张图片

application.yml代码如下:

server:
  port: 8001

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

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

Spring Boot 如何获取配置文件信息_第3张图片

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;
    }
}

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

你可能感兴趣的:(框架,Spring,Boot,Spring,Boot)