问题描述
创建样板项目以向启用 JPA 的数据库公开 RESTful API.它使用以下版本:
- 春季 3.2.6
- 休眠 4.3.0
- 泽西岛 2.5.1
我终于能够让他们一起玩,但仍然存在一些问题.这是最令人费解的事情之一(请参阅 REST 服务类的摘录)
Created boilerplate project to expose RESTful API to JPA enabled database. It's using the following versions:
- Spring 3.2.6
- Hibernate 4.3.0
- Jersey 2.5.1
I finally was able to get them playing together, but still some question remains. Here's one of the most puzzling things (see excerpt from REST service class)
@Service
@Path("resources")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Transactional
public class ResourceServices extends AbstractServices<Resource> {
...
}
如果类被@Service注解,@Transactional注解被忽略并且方法的事务不会启动.但是,当更改为@Component 时,一切正常.不知道为什么.
if class is annotated with @Service, @Transactional annotation is ignored and transaction for the methods is not started. However, when changed to @Component, everything works fine. Couldn't figure out, why.
整个项目可以在这里看到
推荐答案
我也被这个问题搞糊涂了,但最终想通了.
I got puzzled by this as well, but finally figured this out.
jersey-spring 模块只会从您的上下文中导入 @Component
bean.SpringComponentProvider
中确实有一个 beanClass.isAnnotationPresent(Component.class)
检查.
The jersey-spring module will only import @Component
beans from your context. There literally is a beanClass.isAnnotationPresent(Component.class)
check in SpringComponentProvider
.
否则它似乎只创建 bean 的半生不熟的请求范围的实例(我在服务构造函数中使用 Thread.dumpStack
跟踪了这个).他们似乎有依赖注入,但没有 AOP.
Otherwise it appears to only create half-baked request-scoped instances of the bean (I traced this with Thread.dumpStack
in service constructor). They seem to have dependency injection, but not AOP.
泽西岛的问题跟踪器中已经有许多 JIRA 项目:JERSEY-2495,JERSEY-2059、JERSEY-2301
There's a number of JIRA items already in Jersey's issue tracker: JERSEY-2495, JERSEY-2059, JERSEY-2301
更新:我对这些的拉取请求已经合并,这应该在 Jersey 2.11 中修复.
UPDATE: My pull request for these has been merged, this should be fixed in Jersey 2.11.
这篇关于Spring+Jersey 事务注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!