我有多个带有我创建的限定符的类:
@ServiceComponent(restPath = "/trucks")
public class TruckService {
}
@ServiceComponent(restPath = "/cars")
public class CarService {
}
这是限定符(对于问题来说并不重要)
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD})
public @interface ServiceComponent {
public boolean exposeAsRest() default true;
@Nonbinding public String restPath() default "";
@Nonbinding public String restGetPrefix() default "get,find,all";
@Nonbinding public String restPostPrefix() default "create,new,post";
}
在另一个类中,我使用 javax.enterprise.inject.Instance<>
注入(inject)这些实例 class SomeConfigurationClasss {
@Inject
@ServiceComponent()
Instance<Object> _restComponents;
@Override
public void iterate() throws Exception {
//iterate
for(Object obj : _restComponents){
somefuncion(obj);
}
//List.of(_restComponents)
//.flatMap(obj -> somefuncion(obj));
}
}
如果我执行“正常”迭代(对于...),我会得到作为参数传递给 somefunction() 的对象(TruckService 或 CarService)。
但是如果我使用javaslang's List.of(...) 我得到实例本身。我认为这是预期的行为
是否可以在包含一个或多个 bean 的实例上使用 List.of(取决于注入(inject)绑定(bind))。 (我已经尝试在实例上调用 iterator()、select())
最佳答案
Instance<T> extends Iterable<T>
所以你应该使用List#ofAll(Iterable)
关于cdi 实例上的 javaslang List.of(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277113/