Reputation: 91740
Due to another library's requirements, I must define an ApplicationContext
in my main ApplicationContext
with a name of default.context
:
<?xml version="1.0"?>
<beans>
<bean name="default.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>../other-file.xml
</list>
</constructor-arg>
</bean>
<bean class="MyNiceDependency"/>
</beans>
How can I make MyNiceDependency
available to the default.context
context? I'm assuming I need to use the parent
property of ClassPathXmlApplicationContext
but how do I inject the current context to it?
Upvotes: 0
Views: 894
Reputation: 91740
Here's something that should get the job done:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ThisApplicationContextFactoryBean implements FactoryBean<ApplicationContext>,
ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}
@Override
public ApplicationContext getObject() throws Exception {
return getApplicationContext();
}
@Override
public Class<?> getObjectType() {
return ApplicationContext.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
Perhaps there's something better or, better yet, included with Spring?
Upvotes: 1