プログラムでSpringによって定義されたすべてのBeanFactoriesを検出する方法はありますか? Springアプリケーションコンテキストで各Beanの名前とクラスタイプを出力するステータスデバッグページを作成したいと思いますが、すべてのApplicationContextのリストを取得する方法はわかりません。SpringのすべてのBeanFactoryを列挙するにはどうすればよいですか?
答えて
コードをテストすることができ、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
あなたは、あなたのApplicationContext
から豆のすべてを表します渡さConfigurableListableBeanFactory
のBeanDefinition
年代を通過することができますあなたのApplicationContext
とBeanFactoryPostProcessor
を配線することができます。
ConfigurableListableBeanFactory
というこのインスタンスでは、タイプ(getBeansOfType()
)のすべてのBean、または特定の注釈(getBeansWithAnnotation()
)を持つすべてのBeanを見つけることができます。
これを行うには、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();
}
}
しかし、私はどのようにそれがすべての文脈のために働くか、私はそこに知っている私のシステムでは少なくとも2つ、プライマリは1つ、もう1つはspring mvcディスパッチャーserveltによって作成されたものです。 – ams
- 1. ジェネリックインターフェイスを実装するすべてのアイテムを列挙するにはどうすればよいですか?
- 2. Javaで列挙型を別の列挙型に変換するにはどうすればよいですか?
- 3. typeメンバーが列挙型かどうかを調べるにはどうすればよいですか?
- 4. .NETでradiobuttonlistを列挙するにはどうすればよいですか?
- 5. JSオブジェクトのプロパティを列挙できないようにするにはどうすればよいですか?
- 6. DataTableを列挙できるようにするにはどうすればよいですか?
- 7. 列挙型ではなく、列挙型クラスをいつどのように使うべきですか?
- 8. 文字列値で列挙型のオブジェクトを作成するにはどうすればよいですか?
- 9. Nhibernateで列挙型の列を設定するにはどうすればよいですか?
- 10. テーブル列のデータ型を列挙型に変更するにはどうすればよいですか?
- 11. すべての列を同じように大きくするにはどうすればよいですか?
- 12. 行列のすべての列を1つに追加するにはどうすればよいですか?
- 13. Windows XPにインストールされているすべてのアプリケーションを列挙/一覧表示するにはどうすればよいですか?
- 14. 列挙型の値を取得するマクロを作成するにはどうすればよいですか?
- 15. この列挙型からCustomStringConvertibleの説明を取得するにはどうすればよいですか?
- 16. Clojure/postgresql:Jdbc4Arrayの結果から列挙型の値にアクセスするにはどうすればよいですか?
- 17. 列挙型の各メンバーに文字列を関連付けるにはどうすればよいですか?
- 18. C#Unity Dependency Injectionでは、インスタンスの列挙型を取得するにはどうすればよいですか?
- 19. Swagger UIでJavaの列挙型のカスタム値を表示するにはどうすればよいですか?
- 20. 特定の名前の列挙型で値を取得するにはどうすればよいですか?
- 21. 文書列を列挙型に配置するにはどうすればよいですか?
- 22. Scalaの列挙型をパラメータとして渡すにはどうすればよいですか?
- 23. どのようにすべてのデバイスオブジェクトを列挙しますか?
- 24. Rubyの列挙可能なオブジェクトのクラスを取得するにはどうすればよいですか?
- 25. オプションの列挙型定義のスワッガードキュメントを作成するにはどうすればよいですか?
- 26. TypeScriptサービスの列挙型で条件を使用するにはどうすればよいですか?
- 27. Data Lake Storeでファイルのサブセットを列挙するにはどうすればよいですか?
- 28. 特定の列挙を整数で参照するにはどうすればよいですか?
- 29. 特定のパッケージでGroovyクラスを列挙するにはどうすればよいですか?
- 30. ウィンドウフォームアプリケーションで色の列挙を作成するにはどうすればよいですか?
すべてのアプリケーションコンテキストにBeanFactoryPostProcessorを登録する必要がありますか。たとえば、アプリケーションには、Springコンテキストリスナーで初期化されたメインのspring.xmlと、SpringディスパッチャServeltで作成されたもう1つのコンテキストの2つのコンテキストがあります。 – ams
これを追加する必要があるのは、 –
@ams私は間違っていた。 'BeanFactoryPostProcessor'の場合は、アプリケーションコンテキストごとに1つずつ行う必要があります。 http://www.dotkam.com/2008/07/09/spring-web-application-context-visibility/ –