2012-02-10 9 views
1

プログラムでSpringによって定義されたすべてのBeanFactoriesを検出する方法はありますか? Springアプリケーションコンテキストで各Beanの名前とクラスタイプを出力するステータスデバッグページを作成したいと思いますが、すべてのApplicationContextのリストを取得する方法はわかりません。SpringのすべてのBeanFactoryを列挙するにはどうすればよいですか?

答えて

1

コードをテストすることができ、Webアプリケーションのための主要なspring.xmlファイルに登録することができます春のリスナーである、それはのマップを構築しますすべての子アプリケーションコンテキストを返し、このマップをプロパティとして公開します。下記のクラスは、@Autowiredを使用して必要とする任意のスプリングBeanに注入できます。

import java.util.Hashtable; 
import java.util.Map; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationListener; 
import org.springframework.context.event.ApplicationContextEvent; 
import org.springframework.context.event.ContextRefreshedEvent; 
import org.springframework.context.event.ContextStartedEvent; 

public class ContextsApplicationListener implements ApplicationListener<ApplicationContextEvent> { 

    private Map<String,ApplicationContext> contextMap = new Hashtable<String,ApplicationContext>(); 

    @Override 
    public void onApplicationEvent(ApplicationContextEvent event) { 
     if(event instanceof ContextStartedEvent || event instanceof ContextRefreshedEvent){ 
      this.getContextMap().put(event.getApplicationContext().getDisplayName(), event.getApplicationContext()); 
     } 

    } 

    public Map<String,ApplicationContext> getContextMap() { 
     return contextMap; 
    } 
} 

enter code here 
1

あなたは、あなたのApplicationContextから豆のすべてを表します渡さConfigurableListableBeanFactoryBeanDefinition年代を通過することができますあなたのApplicationContextBeanFactoryPostProcessorを配線することができます。

ConfigurableListableBeanFactoryというこのインスタンスでは、タイプ(getBeansOfType())のすべてのBean、または特定の注釈(getBeansWithAnnotation())を持つすべてのBeanを見つけることができます。

+0

すべてのアプリケーションコンテキストにBeanFactoryPostProcessorを登録する必要がありますか。たとえば、アプリケーションには、Springコンテキストリスナーで初期化されたメインのspring.xmlと、SpringディスパッチャServeltで作成されたもう1つのコンテキストの2つのコンテキストがあります。 – ams

+0

これを追加する必要があるのは、 –

+0

@ams私は間違っていた。 'BeanFactoryPostProcessor'の場合は、アプリケーションコンテキストごとに1つずつ行う必要があります。 http://www.dotkam.com/2008/07/09/spring-web-application-context-visibility/ –

1

これを行うには、ApplicationContext対応を使用できます。

 @Component 
     public class PrintSpringBeansInContext implements ApplicationContextAware 
    { 

    private ApplicationContext applicationContext; 

     @Override 
    public void setApplicationContext(ApplicationContext applicationContext) 
     throws BeansException 
    { 

     this.applicationContext = applicationContext; 
    } 


     public void print() 
    { 
     String[] beanNames = this.applicationContext.getBeanDefinitionNames(); 
     StringBuilder printBuilder = new StringBuilder("Spring Beans In Context: ");; 
    for(String beanName : beanNames) 
    { 
     printBuilder.append("\n"); 
     printBuilder.append(" Bean Name: "); 
     printBuilder.append(beanName); 
     printBuilder.append(" Bean Class: "); 
      printBuilder.append(this.applicationContext.getBean(beanName).getClass()); 
    } 
    System.out.println(printBuilder.toString()); 
} 

}

あなたは、以下のこの

@ContextConfiguration(locations={"classpath:context.xml"}) 
    @RunWith(SpringJUnit4ClassRunner.class) 
    public class PrintContextTest 
    { 

    @Autowired 
    private PrintSpringBeansInContext service; 

    @Test 
    public void printBeans() 
    { 
      Assert.assertNotNull(service); 
      service.print(); 
    } 
} 
+0

しかし、私はどのようにそれがすべての文脈のために働くか、私はそこに知っている私のシステムでは少なくとも2つ、プライマリは1つ、もう1つはspring mvcディスパッチャーserveltによって作成されたものです。 – ams

関連する問題