【问题标题】:Spring boot environment variable are not reflecting properlySpring Boot 环境变量没有正确反映
【发布时间】:2018-08-11 01:13:59
【问题描述】:

我正在开发 Spring Boot 应用程序,我想通过配置服务器动态地完全外部化我的环境变量。

所以下面是我写的代码。

Application.java

package com.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.myPackage, com.util")
@PropertySource("classpath:application.properties")
public class Application extends SpringBootServletInitializer {

    static final Logger logger = LoggerFactory.getLogger(Application.class);
    public static ApplicationContext ctx;

    public static void main(String[] args) throws Exception {
        logger.info("Application starting....");
        ctx = SpringApplication.run(Application.class, args);
        logger.info("Application started successfully....");

    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer
    propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

application.properties

server.port=${server.port}    
ENDPOINT_SHAN=${name.mya}

配置服务器:

APPLICATION_NAME=myapp
server.port=8081
name.mya=myName

读取属性

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.apptium.Application;

@Component("configurationProperties")
public class ConfigurationProperties {

    private static Environment prop;

    public static String getConfigValue(String key)   {
        String value = null;
        if (prop == null) {
            prop = Application.ctx.getBean(Environment.class);
        }
        if (prop.getProperty(key) != null) {
            value = prop.getProperty(key).trim();
        }
        if (value == null && System.getenv(key) !=null) {
            value = System.getenv(key).trim();
        }
        return value;
    }
}

所以,当我尝试获取属性 ENDPOINT_SHAN 时,它正在返回 ENDPOINT_SHAN==${name.mya} 但需要返回ENDPOINT_SHAN==myName

还想知道如何正确获取 server.port 的属性。

无论如何,我想知道如何获得ENDPOINT_SHAN 的实际财产。

【问题讨论】:

  • 读取值不是键

标签: java spring spring-mvc spring-boot spring-profiles


【解决方案1】:

您说name.mya 是在“配置服务器”中定义的,但您的代码没有显示与该服务器建立的任何连接。您需要在${name.mya} 变得可解析之前从服务器读取它。

【讨论】:

  • APPLICATION_NAME=myapp server.port=8081 name.mya=myName
  • 添加到配置服务器中。 myapp.properties
猜你喜欢
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2022-01-25
  • 2019-08-03
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多