0
votes

I am working on spring boot application where I want to completely externalize my environment variables dynamically by config server.

So below is the code I have written.

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}

Config Server:

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

To Read Properties

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

so, when I try to get the property ENDPOINT_SHAN, it is returning ENDPOINT_SHAN==${name.mya} but it needs to return ENDPOINT_SHAN==myName

And also am wondering how the property for server.port is taken correctly.

Anyways, I would like to know, how to get an actual property for ENDPOINT_SHAN.

1
read value not key - Y.Kaan Yılmaz

1 Answers

0
votes

You said that name.mya is defined in a "Config Server" but your code doesn't show any connection being established to this server. You need to read it from the server before ${name.mya} becomes resolvable.