java为具有相同名称的多个类实现组件工厂并通过属性/筛选器值进行检索
我正在从事OSGi(Rev 4)项目,在该项目中我必须使用karaf注册表。我可以将实现声明为组件或服务。ie)我可以在卡拉夫向组件工厂/服务工厂注册我的组件/服务,以生产组件/服务对象。 我有多个类实现,所以对于Service factory,我希望可以通过使用不同的属性/筛选器值注册具有相同名称的所有服务来实现单个类(ie)。在检索服务时,我可以使用属性进行查询,这样只有一个检索类就足够了。 项目结构
1. Service(interface)
- implementation 1
- implementation 2 (different implemenations)
- ....
2. Factory(register as a component factory)
3. FactoryManager(which produces objects)
我已经提出了另一个question在SO与我相同的项目结构。有谁能告诉我,这种情况是否可以通过组件工厂(即,使用相同的工厂名(factory="com.java.test.ClassImpl")
注册不同的属性,并通过属性值(动态调用目标属性)检索来实现。
以更精确的方式
第一工厂
@Component(name = "ExampleComponentFactoryServiceProvider", factory = "example.factory.provider")
public class ExampleComponentFactoryServiceProvider implements ExampleFactoryService {
第二工厂
@Component(name = "ExampleComponentFactoryServiceProvider1", factory = "example.factory.provider")
public class ExampleComponentFactoryServiceProvider1 implements ExampleFactoryService {
以上是两个以相同工厂名称注册的工厂。我的期望与不同的财产价值注册。 activate()方法中的检索部分
System.out.println("activate in manager !!!!");
Dictionary<String, String> hashMap = new Hashtable<String, String>();
hashMap.put("component.name", "ExampleComponentFactoryServiceProvider");
instance = factory.newInstance(hashMap);
service = (ExampleFactoryService) instance.getInstance();
System.out.println("service = " + service.toString());
Dictionary<String, String> hashMap1 = new Hashtable<String, String>();
hashMap1.put("component.name", "ExampleComponentFactoryServiceProvider1");
instance = factory.newInstance(hashMap1);
service = (ExampleFactoryService) instance.getInstance();
System.out.println("Service = " + service.toString());
在上面的代码中,组件工厂试图通过筛选属性来创建两个不同的实例。但它会创建第一个,如果第一个停止,它会创建第二个。如何通过运行时生成的具有不同目标属性的相同工厂id创建对象
共 (0) 个答案