I don't know if it is the better way to implement my solution:
@Component
@Scope("singleton")
public class GetFromJson implements Serializable{
/**
*
*/
private static final long serialVersionUID = -8203722696439228358L;
Map<String, Map<String, ArrayList<String>>> jsonMap;
public Map<String, Map<String, ArrayList<String>>> getJsonMap() {
return jsonMap;
}
public void setJsonMap(Map<String, Map<String, ArrayList<String>>> jsonMap) {
this.jsonMap= jsonMap;
}
}
The class is Serializable because I get the content from a Json from a database and I map the content in an Object GetFromJson (I need do the query in database only one time). For this reason I need use the Object in all my APp, for this reason I think that I need use a Singleton
Somebody publish to me a library to get the JSON from database. the JSON object parse to Map<String, Map<String, ArrayList<String>>>
for this reason I create my GetFromJson
class.
Now I have many rest Service, in my rest services I need use my object: GetFromJson to get the content of jsonMap.
I have many questions.
With the annotation @Scope("singleton")
I guarantee have the GetFromJson
only one instance available in all my app?
Or what is the better way to have in Spring a singleton and session object available in my app?
To access to get the content of jsonMap of GetFromJson is enough use
@Autowired
private GetFromJson jsonObject;
Or how can I get my singleton class?
and in my rest method service use:
jsonObject.getJsonMap();
How can Initialize my singleton, because i am trying initialize my object with:
@Autowired
private GetFromJson jsonObject;
public methodToInitialize(){
jsonObject = methodLibraryFromDatabase(GetFromJson.class);
}
ANd the IDE tell to me that the field initialization is not used
I need the same logic, but with session Object, I think that if I understand the singleton implementation with my session object will be the same but only changing the anotation to: @Scope("session")