1

假设我有这样的课程:

@References(
        value={
                @Reference(name="dataSources",cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE,policy=ReferencePolicy.DYNAMIC,strategy=ReferenceStrategy.EVENT,bind="bind",unbind="unbind",referenceInterface=DataSourceProvider.class)
        })      
public class DataSourceStoreServiceImpl implements DataSourceStoreService {

    List<DataSourceProvider> dataSourceProviders = new CopyOnWriteArrayList<DataSourceProvider>();

    public void bind(DataSourceProvider dataSourceProvider) {
        dataSourceProviders.add(dataSourceProvider);
    }

    public void unbind(DataSourceProvider dataSourceProvider) {
        dataSourceProviders.remove(dataSourceProvider);
    }

}

这就是我处理具有MULTIPLE基数的裁判的方式。

我的问题是是否可以在没有样板的情况下做到这一点?我的想法是这样的:

public class DataSourceStoreServiceImpl implements DataSourceStoreService {

    @CollectionType(CopyOnWriteArrayList.class)
    @Reference(name="dataSources",cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE,policy=ReferencePolicy.DYNAMIC,strategy=ReferenceStrategy.EVENT,bind="bind",unbind="unbind",referenceInterface=DataSourceProvider.class)
    List<DataSourceProvider> dataSourceProviders

}
4

1 回答 1

0

使用IPojo 注释

只需使用注释“requires”声明一个数组,如下例所示:

@Requires
private Hello[] m_hello; // Service Dependency

然后你可以这样调用你的服务:

for(Hello helloService: m_hello){
       helloService.ohHai("Test");
}

哦,我忘了,您的提供者类需要 2 个注释:@Component并且@Provides 您的消费者类需要@Component@Instantiate

于 2014-04-23T15:20:00.483 回答