ClasspathXmlApplicationContext与当前ApplicationContext的父级?

时间:2012-08-06 21:36:14

标签: spring spring-3

由于其他图书馆的要求,我必须在我的主ApplicationContext中定义名为ApplicationContext的{​​{1}}:

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> 提供给MyNiceDependency上下文?我假设我需要使用default.context的{​​{1}}属性,但如何将当前上下文注入其中?

1 个答案:

答案 0 :(得分:1)

这是应该完成工作的事情:

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;
    }
}

也许有更好的东西,或者更好的,包含在Spring中?