[英]Tapestry 5 and Spring beans with same interface
我对Tapestry 5和Spring集成有问题。 如果我有多个实现相同接口的bean,并且尝试使用@Inject
注释注入它们,则会出现问题。 我当然有例外。
我发现一个教程说,在这种情况下,我也必须使用@Service
批注,但是现在
org.apache.tapestry5.internal.services.TransformationException
Error obtaining injected value for field
com.foo.pages.Foo.testService: Service
id 'someServiceIDeclaredInSpringContextFile' is not defined by any module...
无论如何,问题是:如何将两个实现相同接口的不同的spring bean注入Tapestry 5页面?
我解决了这个问题。
首先,我做了一个新的注释
public @interface Bean {
String value();
}
每当我有多个实现相同接口的bean中的一个时就使用它
@Inject
@Bean("springBeanName")
Service foo;
然后我更改了org.apache.tapestry5.internal.spring.SpringModuleDef
private ContributionDef createContributionToMasterObjectProvider() {
....
public void contribute(ModuleBuilderSource moduleSource,
ServiceResources resources,
OrderedConfiguration configuration) {
....
switch (beanMap.size()) {
case 0:
return null;
case 1:
Object bean = beanMap.values().iterator().next();
return objectType.cast(bean);
default:
Bean annotation = annotationProvider.getAnnotation(Bean.class);
Object springBean = null;
String beanName = null;
if (annotation != null) {
beanName = annotation.value();
springBean = beanMap.get(beanName);
} else {
String message = String.format(
"Spring context contains %d beans assignable to type %s: %s.",
beanMap.size(),
ClassFabUtils.toJavaClassName(objectType),
InternalUtils.joinSorted(beanMap.keySet()));
throw new IllegalArgumentException(message);
}
if (springBean != null) {
return objectType.cast(springBean);
} else {
String message = String.format(
"Bean [%s] of type %s doesn't exists. Available beans: %s",
beanName, ClassFabUtils.toJavaClassName(objectType),
InternalUtils.joinSorted(beanMap.keySet()));
throw new IllegalArgumentException(message);
}
}
}
};
听起来您要么在@Service批注的名称中输入了错字,要么实际上并没有使用期望的名称定义bean。 没有更多信息,很难确定,因为还有其他可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.