1

edit: please pay close attention to the question. I want to make changes without having to rebuild and redeploy the application. I want to make changes on the fly.

I have a simple Spring boot application where I am trying to test if an application can read environment variable without having to rebuild and redeploy the application.

I have a simple main class which is also a @RestController

@SpringBootApplication
@RestController
@EnableScheduling
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Value("${taco.orders.pageSize}")
    private String pageSize;

    @GetMapping("/myName")
    public String myName() {
        return pageSize;
    }

    int i = 0;

    @Scheduled(fixedRate = 2000L)
    public void scheduled() {
        System.err.println(++i + "-" + pageSize);
    }
}

This is what I have in my application.yml file:

taco:
  orders:
    pageSize: fifty

This print fine "fifty". But when I go to terminal and set a different value for the key, that new value is not reflected.

export TACO_ORDERS_PAGESIZE=NINETY

I have also created a git repo if somebody wants to retry it.

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • https://stackoverflow.com/questions/13248066/how-to-reload-properties-with-spring – Ryuzaki L Jul 08 '19 at 02:44
  • Possible duplicate of [How to reload properties with Spring?](https://stackoverflow.com/questions/13248066/how-to-reload-properties-with-spring) – Ryuzaki L Jul 08 '19 at 02:45

1 Answers1

2

You need to substitute the environment variable in your yaml file.

taco: 
     orders: 
        pageSize: {TACO_ORDERS_PAGESIZE : fifty}

The default value for your taco.orders.pageSize is fifty it will be automatically override by your env variable TACO_ORDERS_PAGESIZE NINETY.

GnanaJeyam
  • 2,780
  • 16
  • 27
  • It is not working. I am not passing those env variables when booting up the application. I am passing those env variables when the application is already running. I don't want to rebuild and redeploy the application. I want to change those env variables dynamically during runtime. – Faraz Jul 07 '19 at 19:24
  • I think for this you may need to refer @RefreshScope logic. Refer : https://andressanchezblog.wordpress.com/2016/09/15/refresh-scope-in-spring-cloud/ – GnanaJeyam Jul 08 '19 at 09:42