我在Tapestry 5和
Spring集成方面遇到了问题.如果我有一个实现相同接口的多个bean并尝试使用@Inject注释注入它们,则会出现问题.当然我得到了一个例外.
我找到了一个tutorial,说在那种情况下我也必须使用@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);
}
}
}
};