I am using Spring Framework 4.
I have a class (say ClassA) in which another class (say ClassB) is been used. ClassA's member variables are getting value from ClassB. ClassB has a static method which read data from properties file. In ClassB a static member variable ApplicationContext
is been injected using @Autowired
annotation.
What I want is, I want to make sure that when ClassA uses's its member variable it should get all set with values read from properties file. And for that ClassB should get ApplicationContext
all set to read from MessageSource
.
As ClassA is marked as @Component
, Spring loads ClassA, but when it tries to initialize member variables, it is getting NullPointerException
, as ApplicationContext
is not yet initialized.
So my question here is, Is there any way available to let Spring tell that some bean should be initialized at some order or something like that. I tried using @DependsOn
annotation and specified @Bean
to the getter method of ApplicationContext
. But it gives below exception:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'applicationContext': Requested bean is currently in creation: Is there an unresolvable circular reference?
Any idea on this issue?
Thanks
Thanks for comments.
I found the solution in one of the annotations supplied by Spring Framework.
Solution of @DependsOn
annotation worked. Actually when I was using @Bean above getter method of ApplicationContext
it was firing exception as mentioned in the question. Then I read the documentation of @DependsOn
annotation. It states that this annotation is applicable on @Bean and @Component. As the class in which ApplicationContext
was being injected, I've made ClassA @DependsOn
the @Component
class where ApplicationContext
is being injected and it works.
Thanks again for your comments.