Tapestry 5 and Spring beans with the same interface

I have a problem integrating Tapestry 5 and Spring. The problem occurs if I have several beans that implement the same interface, and I try to enter them using the @Inject annotation. Of course, I got an exception.

I found a tutorial that says in this case I have to use the @Service annotation, but now I get

 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... 

In any case, the question is: how can I insert two different Spring beans that implement the same interface into the Tapestry 5 page?

+4
source share
2 answers

I solved this problem.

First I made a new annotation

 public @interface Bean { String value(); } 

and I use it wherever I have one of several beans that implement the same interface

 @Inject @Bean("springBeanName") Service foo; 

Then I changed 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); } } } }; 
+2
source

It looks like you either have a typo in the name that you specify in the @Service annotation, or you really haven't defined a bean with the name you expect. Without additional information, it is difficult to say for sure, since there are other possibilities.

0
source

Source: https://habr.com/ru/post/1307558/


All Articles