ServiceLocator
アプリケーションコンテキストをレジストリとして使用したいようです。
クライアント コードを Spring に結合せずにキーを Bean 名にマッピングする ServiceLocator を作成するには、ServiceLocatorFactoryBeanサポート クラスを参照してください。
その他のオプションは、命名規則または注釈ベースの構成を使用することです。
たとえば、Service に で注釈を付けると仮定すると@ExampleAnnotation("someId")
、次の Service Locator のようなものを使用してそれらを取得できます。
public class AnnotationServiceLocator implements ServiceLocator {
@Autowired
private ApplicationContext context;
private Map<String, Service> services;
public Service getService(String id) {
checkServices();
return services.get(id);
}
private void checkServices() {
if (services == null) {
services = new HashMap<String, Service>();
Map<String, Object> beans = context.getBeansWithAnnotation(ExampleAnnotation.class);
for (Object bean : beans.values()) {
ExampleAnnotation ann = bean.getClass().getAnnotation(ExampleAnnotation.class);
services.put(ann.value(), (Service) bean);
}
}
}
}