[英]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.