Is there any way to reference the current application context in a bean config file in Spring?
I am trying to do something like this:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="some-bean-name" class="com.company.SomeClass">
<constructor-arg>
<!-- obviously this isn't right -->
<bean ref=#{this}/>
</constructor-arg>
</bean>
The issue is that SomeClass needs an ApplicationContext instance in its constructor. Is there any way to get the reference of the ApplicationContext that is loading the beans? I know that I can do all of the loading in the XML, but that is not quite what I am after as I need to do the bean loading in my java code.
Have you looked at implementing
ApplicationContextAware? It doesn’t come in on the constructor, but it does happen before aninit()call, and will happen just after bean properties are populated.You can also just
@Autowire(d)it if using Spring 2.5 or later.Of course, doing either of these will tie your code to Spring.