繁体   English   中英

如何让jersey Web服务类实现接口并进行事务处理

[英]How to have a jersey web service class implement interface and be transactional

我想使用Jersey和Spring编写一个Java类,该类既是Web服务服务器端类,也可以与spring-tx进行事务处理(这样,每个Web服务请求要么完全完成其在db中的工作,要么完全回滚它的在数据库中工作)。

但是,当我这样做的时候...

package com.test.rest

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component 
@Transactional
public class TestRestService implements TestRestServiceInterface {
    ...
}

Spring并未将TestRestService类注册为Web服务类。

我在spring config文件中使用<context:component-scan base-package="com.test.rest"/>在com.test.rest包中注册了Web服务类(示例中的包名称已更改)。

如果我删除@Transactional或让TestRestService不实现接口,则该类将由Spring注册为Web服务类,并且代码可以正常工作。

有办法让我同时拥有两者吗?

我目前正在使用sprint-tx,spring-jersey和spring-context 3.0.7和jersey 1.0.3.1

经过大量研究后,我得出的结论是它将无法工作(也许没有AspectJ)。

我相信由于jersey-server-1.0.3.1:com.sun.jersey.api.core.ResourceConfig.java中的这段代码,它将无法正常工作

438  /**
439   * Determine if a class is a root resource class.
440   *
441   * @param c the class.
442   * @return true if the class is a root resource class, otherwise false
443   *         (including if the class is null).
444   */
445  public static boolean isRootResourceClass(Class<?> c) {
446      if (c == null)
447          return false;
448      
449      if (c.isAnnotationPresent(Path.class)) return true;
450  
451      for (Class i : c.getInterfaces())
452          if (i.isAnnotationPresent(Path.class)) return true;
453  
454      return false;
455  }

出于某种原因,当我们在实现接口的类上使用@Transactional时,spring-tx生成的Proxy类(无论是基于CGLIB还是基于JDK动态代理)没有@Path注释,因此isRootResourceClass返回false,而该类不是注册为网络服务类。 我在通过代码调试时验证了这一点。

我想我只需要在实现接口或使我的Web服务类具有事务性之间进行选择即可(除非我使用AspectJ)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM