0

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.

bpgergo
  • 15,669
  • 5
  • 44
  • 68
Karusmeister
  • 871
  • 2
  • 12
  • 25

4 Answers4

2

If you have a factory method in your code, then make xml configuration call that factory method instead of a constructor. DO NOT call getInstance from your Java code.

<bean id="fromFactory" class="org.example.MyFactory" factory-method="getInstance" />
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

Spring will create a single instance of your class by default. It will call the class's constructor once. I think you have confused this with the

public static void getInstance() 

idiom for singletons in Java which is an attempt to enforce in your class that you can never have more than one instance.

Spring has constructed a single instance of your class and is storing it in the Spring container ready for you to use. To get a reference to the instance Spring has created, you need to retrieve it from Spring's application context.

serg10
  • 31,923
  • 16
  • 73
  • 94
1

I think this could be relevant to your question: Why is Spring's ApplicationContext.getBean considered bad?

There's a number of ways: you can get your bean instance from ApplicationContext, you can @Autowire it, etc.

Community
  • 1
  • 1
maksimov
  • 5,792
  • 1
  • 30
  • 38
0

If your class implements the Singleton Pattern then there is no way getInstance() will return an instance other then what Spring has created.

how can I get reference to the singleton created during initialization

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.

Community
  • 1
  • 1
bpgergo
  • 15,669
  • 5
  • 44
  • 68
  • Thanks a lot! I think that is what I was looking for but have to have a look at link given maksimov below. – Karusmeister May 24 '12 at 12:12
  • Sure. Just note, that I have given you this same link (the last one). – bpgergo May 24 '12 at 12:21
  • @maksimov, I have checked and figured that [you had posted your answer several minutes before I posted mine](http://data.stackexchange.com/stackoverflow/query/71138/who-answered-first?MyUserId=176569&OtherUserId=211197). However, believe me, I had not seen your answer before I posted mine. We just independently concluded, that this is a relevant link -- which is a good thing. – bpgergo May 25 '12 at 13:12
  • @bpgergo there was a smiley in my comment after all. it's all for the common good so don't worry (i don't). – maksimov May 25 '12 at 13:14