使用@Resource注释的SessionContext注入

ane*_*rgy 2 java java-ee ejb-3.0 java-ee-6

我需要回滚EJB 3无状态SessionBean(CMT,JBoss版本5),我正在使用它

sessionContext.setRollbackOnly();
Run Code Online (Sandbox Code Playgroud)

使用@Resource注释注入此sessionContext.我的问题:1)是否首选在EJB3中回滚?

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

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

但以下工作正常:

@Resource
private SessionContext sessionContext;
Run Code Online (Sandbox Code Playgroud)

以下是第一种情况的例外情况:

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
Run Code Online (Sandbox Code Playgroud)

Bre*_*ail 6

我假设EJB是一个@WebService,这就是你得到JAXB错误的原因.尝试:

@Resource
@WebMethod(exclude=true)
public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
}
Run Code Online (Sandbox Code Playgroud)

或者,更改方法可见性或添加最终修饰符(仅公共非最终非静态方法是webservices方法).