-1

I have couple of params let's say value1 and value2 which i need to store throughout the lifecycle of an application, can this be achieved? I have two different modules in module1 i have a utility class which comprises of value1 and value2 and this module added as a dependency in module2. I will get the values of value1 and value2 from external service call(i don't want to hit them everytime to get these values). If i create a method in utility class which return these values and mark it as @Caheable will it be there forever(till we retart or redeploy the application) or is there any internal time limit on cacheable. Would u pls suggest some way of storing this?

I have tried this way (How to reinitialize a Spring Bean?), created a Config class(annotated with @Configuration) and created

Module1 - Config.java

@Bean("utilitybean")
public Utility getValues(){
return new Utility("dummyvalue1","dummyvalue2");
}

here using this approach to destroy the bean and create with value1 and value2(methodname-regiter) How to reinitialize a Spring Bean?

public Utility getNewValues(){
return getValues();
}

Module2 - Test.Java

In module2 i have autowired the class 

@Autowired
private Config config;

//service call to get the new values
once i get the new values i am calling config.regiter to re-register with new values.

But when i am calling config.getNewValues().. i am still getting the old values.

(i think this is happening bcoz of two different contexts)

Would u pls suggest any other way to store these values throughout the lifecycle of an application?

NOTE: there are 2 other modules which will utilize these new values for other service calls

Micro
  • 17
  • 3

2 Answers2

0

If i understand correctly, you want to use value1 and value2 as enviroment variables. If i remember correctly, there are two ways to pass enviroment variables to a spring boot app, that will live in it's lifecycle as you mention. The first one is, by adding those values in a .properties file (or application.properties) or in a yml file (or application.yml) depending on your set up. You can also pass them externally, when running the application as jar (provided that you use the embedded tomcat server). Please check the links below

https://docs.spring.io/spring-boot/docs/1.3.3.RELEASE/reference/html/boot-features-external-config.html

Pass environment variables to Spring Boot application with Maven

The second option can be viable, if you use a script that gets the desired values from the mentioned external source, and pass them as arguments when it tries to execute the jar.

  • those are credentials.. i have been asked to not set them as env variables or system properties, and not in properties file too – Micro Feb 08 '23 at 02:56
0

Just a simple solution. Create a class with 2 variables.

Add the @Component Annotation at the class.

Set the values of the variables, as per your startegy or use PostConstruct method strategy to set the values

@Component gives guarantee, the values once persisted, available until app shutdown.

On restart you have to persist the values once again

Make sure the @Component class is the spring scan path.

Prakash Ayappan
  • 455
  • 2
  • 8