java 使用@Resource 注解的 SessionContext 注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5538785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:36:04  来源:igfitidea点击:

SessionContext Injection using @Resource annotation

javajakarta-eeejb-3.0java-ee-6

提问by anergy

I need to rollback in EJB 3 Stateless SessionBean (CMT, JBoss version 5), for which I am using

我需要回滚我正在使用的 EJB 3 Stateless SessionBean(CMT,JBoss 版本 5)

sessionContext.setRollbackOnly();

This sessionContext is injected using @Resource annotation. My questions: 1) Is it preferred way to rollback in EJB3?

这个 sessionContext 是使用 @Resource 注释注入的。我的问题:1)它是在 EJB3 中回滚的首选方式吗?

2) Why Jboss complains on deployment if I use public setter injection

2) 如果我使用公共 setter 注入,为什么 Jboss 会抱怨部署

// throws exception on deployment.
    private SessionContext sessionContext;
    @Resource
    public void setSessionContext(SessionContext sessionContext) {
     this.sessionContext = sessionContext;
    }

but following works fine:

但以下工作正常:

@Resource
private SessionContext sessionContext;

Here is the exception in first case:

这是第一种情况的例外:

javax.ejb.SessionContext is an interface, and JAXB can't handle interfaces.
        this problem is related to the following location:
                at javax.ejb.SessionContext
                at public javax.ejb.SessionContext invoice.sap.service.jaxws.SetSctx.arg0
                at invoice.sap.service.jaxws.SetSctx
javax.ejb.SessionContext does not have a no-arg default constructor.
        this problem is related to the following location:
                at javax.ejb.SessionContext

回答by Brett Kail

I assume the EJB is an @WebService, which is why you're getting JAXB errors. Try:

我假设 EJB 是一个 @WebService,这就是您收到 JAXB 错误的原因。尝试:

@Resource
@WebMethod(exclude=true)
public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
}

Alternatively, change the method visibility or add the final modifier (only public non-final non-static methods are webservices methods).

或者,更改方法可见性或添加 final 修饰符(只有公共非最终非静态方法是 Web 服务方法)。

回答by Heiko Rupp

1) yes

1) 是的

2) Dunno, perhaps a bug, perhaps deprecated. I've glanced through the EJB 3.1 spec and there I only saw the @Resource SessionContext sessionContextform, while the EJB 3.0 spec also showed the setter injection.

2)Dunno,也许是一个错误,也许已被弃用。我浏览了 EJB 3.1 规范,在那里我只看到了@Resource SessionContext sessionContext表单,而 EJB 3.0 规范还显示了 setter 注入。