2012-08-06 21 views
0

由於另一個庫的要求,我必須定義在我的主ApplicationContextApplicationContextdefault.context名稱:ClasspathXmlApplicationContext與當前ApplicationContext的父級?

<?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背景?我假設我需要使用ClassPathXmlApplicationContextparent屬性,但是如何將當前上下文注入它?

回答

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中的?

相關問題