I'm new to Spring and thus apologies in advance if the questions seems to be trivial.
When I declare a bean in spring it is singleton by default. When spring initializes beans from config.xml it's using default creator. If I declare my private creator and getInstance method for a class, I don't get reference to the bean created during Spring initialization - I simply create same class again and this class is referenced when getInstance() is called any time later.
My question is how can I get reference to the singleton created during initialization (to the bean defined in config.xml) from code.
If your class implements the Singleton Pattern then there is no way
getInstance()
will return an instance other then what Spring has created.Basically, you should have it injected to the other class, where you will need it. And you can also refer to it by
ApplicationContext.getBean()
, although it is not that elegant.