Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tapestry 5和Spring Bean具有相同的接口_Java_Spring_Dependency Injection_Tapestry - Fatal编程技术网

Java Tapestry 5和Spring Bean具有相同的接口

Java Tapestry 5和Spring Bean具有相同的接口,java,spring,dependency-injection,tapestry,Java,Spring,Dependency Injection,Tapestry,我对Tapestry 5和Spring集成有问题。如果我有一个实现相同接口的多个bean,并且我尝试用@inject注释注入它们,就会出现问题。当然我有个例外 我发现了一个说明,在这种情况下,我也必须使用@Service注释,但现在我得到了 org.apache.tapestry5.internal.services.TransformationException Error obtaining injected value for field com.foo.pages.Foo.testSe

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

无论如何,问题是:如何将实现同一接口的两个不同的SpringBean注入Tapestry 5页面?

听起来可能是@Service注释的名称有误,或者您没有实际使用预期的名称定义bean。没有更多的信息,很难确定,因为还有其他一些可能性。

我解决了这个问题

首先我做了一个新的注解

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);
             }
           }
         }
       };

不,仔细检查-没有输入错误,在commonBeans.ctx.xml中,我发现Spring集成的bean-tooth行为在5.0版和5.1版之间发生了变化。在5.0中,每个SpringBean都作为Tapestry服务公开,如果有两个bean实现同一个接口,则始终可以使用带有适当bean名称的
@service
来澄清您所指的对象。在5.1中,情况不再如此,使得
@Service
注释对Springbean没有用处。除非你已经准备好自己做一些元编程魔术,否则你就被套住了。顺便说一句,很好的解决方法。