7

I want to store a particular variable (String or Object) in application context in spring boot application. But I don't want to use an xml or properties file for storing it.

There will be a function which will store the data in application context. I should be able to retrieve it, modify it, delete it or add more data.

Basically I want to store data in application context after its initialization has been done.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
anu l
  • 111
  • 1
  • 2
  • 7
  • 1
    You could obtain an instance of the `ApplicationContext` and `register(Class>... annotatedClasses)` needed classes, but context refresh is required. – Andrew Tobilko Apr 23 '17 at 18:57
  • I mean.., this is what spring (container)does. – AchillesVan Apr 23 '17 at 19:35
  • thanks Andrew, so you are saying using register I can add an instantiated object to applicationcontext?. Is there an example where this is illustrated? The values of these objects stored must come from java code or user, but not xml or properties file. – anu l Apr 24 '17 at 02:07

2 Answers2

5

If you create a class and put @Configuration annotation over it, and declare some beans with @Bean annotation, they become application managed beans.

@Configuration
public class ConfigurationClass {

    @Bean("myString")
    public String getString() {     
        return "Hello World";
    }
}

Then anywhere from your code you can call AnnotationConfigWebApplicationContext.getBean("myString") to get your bean.

Now there could be concurrency issues (as mentioned by davidxxx ). For that you can use SimpleThreadScope. Look at this link to learn how to register it with application context and where to place that annotation (or google it).

Or if you don't want to deal with that, then a @Scope("request") should help. The bean with @Scope("request") would be created for every new incoming request thus it’s thread safety is guaranteed since it’s created every time a new request comes in.

Faraz
  • 6,025
  • 5
  • 31
  • 88
2

As a general way, you should avoid creating stateful services.
Storing application data in the server memory makes it harder to be resilient, autonomous but also distributed and scalable.

Besides, Spring Beans are not designed to be stateful.
If you do it, you may have race conditions without the required synchronization.

About your need, if you want really to have a stateful service at startup, create your own singleton instance and synchronize the methods that should be thread safe.
Then access it from anywhere.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Hello :) Yes they become. The problem is the concurrency : the op wants to store/modify/get data from it. – davidxxx Apr 23 '17 at 19:17
  • @Faraz Durrani We could believe but I don't think as `@Scope(value = "request")` will create a distinct instance by HTTP request. If you are interested in this matter, you can look at the accepted answer here: http://stackoverflow.com/questions/15745140/are-spring-objects-thread-safe – davidxxx Apr 23 '17 at 20:11