Lav
Lav

Reputation: 1313

Practical use of ApplicationContextAware in spring

I have had chance of working on only one project using spring , and the way it worked was

  1. Make a singleton class (lets say MySpringHelper), that has method like getBean(String beanName)
  2. What getBean(String) does is, it first checks existence of applicationContext, if it exists uses same to get the bean , else creates new applicationContext and returns the bean
  3. Wherever in you project you need a bean simply call MySpringHelper.getBean("abc")

Keeping this in mind , when i was studying spring , i noticed interface "ApplicationContextAware" ... I am not sure when will this be needed, uses above pattern such interface seems not of any use. Or the above Singleton MySpringHelper pattern/approach is incorrect ??

Looking forward to learn from your experience

To give more details on application , its like a pdf file generator, 1 pdf file having 12-15 different charts, so the main method runs 1 thread for each chart , and inside these chart logic we are using singleton MySpringHelper

Upvotes: 0

Views: 1286

Answers (1)

SMA
SMA

Reputation: 37023

Why are you checking the existance of applicationContext? It should be there if your helper bean is configured in xml and has setter method in it. There is no need to create application context in that case.

For your case, I would suggest you get applicationContext injected by Spring rather than by using ApplicationContextAware.

Upvotes: 1

Related Questions