【问题标题】:What is the replacement of EJB SessionContext object in spring boot?Spring Boot 中 EJB SessionContext 对象的替换是什么?
【发布时间】:2021-12-07 21:45:57
【问题描述】:

我正在将 EJB 项目迁移到 Spring Boot 项目。我已成功将其他注释替换为 spring 注释,但 SessionContext 对象存在问题。 我的遗留代码如下

@Resource
SessionContext sessionContext;
.....
if (some condition) {
    sessionContext.setRollbackOnly();
    return false;
}

对于此代码,我收到以下错误

A component required a bean of type 'javax.ejb.SessionContext' that could not be found.


Action:

Consider defining a bean of type 'javax.ejb.SessionContext' in your configuration.

【问题讨论】:

    标签: spring spring-boot session spring-session sessioncontext


    【解决方案1】:

    我认为您必须使用一些不同的功能。

    setRollbackOnly()

    我经常看到用于回滚的会话上下文。在 Spring 中,您可以将其替换为:

    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    

    或用

    注释类
    @Transactional(rollbackFor = MyException.class) 
    

    这样你就可以从类中抛出你的异常来导致回滚。

    getBusinessObject()

    第二个最常用的功能是加载业务对象的方法,例如,我可以在同一个 bean 中创建一个新事务。在这种情况下,您可以使用自注入:

    @Lazy private final AccountService self;
    

    并使用@Transactional 注释方法。当然,这解决了您需要使用代理对象功能的任何其他情况。

    其他功能由 Spring 中的其他类提供,但我认为这两个是 Java EE 世界中最常用的,当迁移时,人们会寻求在 Spring 中替换它们。

    【讨论】:

    • 我正在编辑的类不包含任何@Transaction 注释,而是调用其他包含@Transaction 注释的类的方法。这是一个问题吗?
    • 我觉得应该没问题。交易已经被之前的方法激活了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2013-03-02
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多