在CDI bean中使用Spring Data Repo时,启动挂起 [英] Startup hangs when using Spring Data repo inside CDI bean

查看:106
本文介绍了在CDI bean中使用Spring Data Repo时,启动挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临以下问题-尝试从CDI bean内部访问Spring Data存储库时,我的应用程序在启动时挂起(未给出错误).这是我的仓库:

I am facing the following issue - my application hangs on startup (no error is given), when trying to access the Spring Data repository from inside the CDI bean. This is my repo:

import org.springframework.data.jpa.repository.JpaRepository;

public interface ConfigValueRepository extends JpaRepository<ConfigValue, ConfigValueKey> {
}

其中ConfigValueKey:

where ConfigValueKey:

import java.io.Serializable;

class ConfigValueKey implements Serializable {
    private Long keyId;
    private Long node;
}

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import node.HierarchyNode;

@ApplicationScoped
public class ConfigurationSetter {

    private ConfigValueRepository configValueRepository;

    @Inject
    public ConfigurationSetter(ConfigValueRepository configValueRepository) {
        this.configValueRepository = configValueRepository;
    }

    public void init(@Observes @Initialized(ApplicationScoped.class) Object o) {
        String exportLocationVar = System.getProperty("EXPORT_LOCATION");
        if (StringUtils.isNotBlank(exportLocationVar)) {
            configValueRepository.findById(ConfigValueKey.of(3050578512872244649L, HierarchyNode.DATACENTER_ID))
                    .ifPresent(v -> {
                        v.setValue(exportLocationVar);
                        configValueRepository.save(v);
                    });
        }
    }
}

有什么想法吗?我在JBoss上运行该应用程序.

Any ideas? I run the application on JBoss.

推荐答案

当前,使用CDI配置Spring Data Jpa有点棘手.它没有涵盖所有典型场景. 上个月,我向Spring Data项目提出了一些有关CDI配置的拉动请求,我打开了另一个请求来改善Spring Data的组成,但直到现在我都没有关于最后一个的消息. 我将在Spring Data项目中填写另一个问题,以讨论对更好的配置和CDI集成的支持,并且为解决一些典型的集成问题创建了一个示例项目.

Currently Spring Data Jpa is a little tricky to configure with CDI. It's not covering all typical scenarios. Last month, I pushed a little pull request to the Spring Data project about CDI configuration and I had opened another to improve Spring Data composition but I haven't news until now with last one. I am filling another issue in the Spring Data project to discuss about support for a better configuration and CDI integration and I made a sample project for that solving some typical integration problems.

如果您想将Spring Data Jpa与CDI一起使用,我真的建议您查看我的github项目.这是一个实验,但是如果他们接受,我希望将此更改发送到Spring Data项目.

If you want to use Spring Data Jpa with CDI, I really recommend you to see my github project. It's an experiment but I wish to send this changes to Spring Data project if they accept them.

看看(( https://github.com/arielcarrera/cdi-spring-data-jpa-test ).

该项目涵盖具有CDI(焊接),JTA(Narayana)和JPA(休眠)集成的Spring Data Jpa.它具有广泛的测试,并且可以像人们期望的那样运行良好.

This project covers Spring Data Jpa with CDI (Weld), JTA (Narayana) and JPA (Hibernate) integration. It has a big spread of tests and it is working good as one expects.

以另一种方式,我建议您注意以下几点:

In another way, I suggest you to take care about some points:

  • CDI默认情况下具有延迟初始化,并且在Spring的启动时使用.因此,您需要检查将@Eager放到存储库中,以在启动时强制进行初始化.
  • 重要的是要注意实体管理器的范围.目前,只有@Dependent伪作用域可用于默认的Spring Data Jpa实现,因为它在启动时使用了实体管理器,例如.发现元数据,查询并建立存储库. 这将限制您的体系结构设计,事务管理和持久性上下文的共享.
  • 如果要使用声明式事务划分(@Transactional),可以查看我的项目以检查如何配置自定义Tx拦截器.
  • 如果您想在存储库层之上拥有一个事务服务层,那么应该看看我对Spring Data JPA所做的更改.伪依赖范围的bean有一些限制,您需要考虑使用@RequestScoped或@TransactionalScoped实体管理器bean,以便在存储库实例之间(以及内部)共享持久性上下文.默认的Spring Data JPA实现(使用@Eager批注)在启动时使用一个entitymanager,因此RequestScoped不起作用.因此我在实验中解决了更改spring数据代码的问题.
  • 我目前不建议您将@RequestScoped与JTA(Narayana)和CDI一起使用.在 https://developer.jboss.org/message/990406 中发现了一个交易问题和@TransactionalScoped现在效果更好.
  • 检查您的日志记录配置是否正常运行,并将日志记录设置为调试级别.
  • CDI has lazy initialization by default and uses to be at startup in Spring. So you need to check to put @Eager to your repositories to force initialization at startup.
  • It is important to take care about entity manager scope. Only @Dependent pseudo-scope will work at this moment with the default Spring Data Jpa implementation because it uses entity manager at startup eg. to discover metadata, queries and build the repositories. This point will limit your architecture design, transaction management and sharing of the persistence context.
  • If you want to use declarative transactional demarcation (@Transactional) you can take a look to my project to check how to configure a custom Tx Interceptor.
  • If you want to have a transactional service layer over repository layer, you should take a look to my changes to Spring Data JPA. A pseudo dependent scoped bean has some limitations and you will need to take in consideration a @RequestScoped or @TransactionalScoped entity manager bean in place to share a persistence context between repositories instances (and inside too). Default Spring Data JPA implementation (using @Eager annotation) uses an entitymanager at startup, so a RequestScoped does not work. So I resolved it changing spring data code in my experiment.
  • I don't suggest you to use @RequestScoped at this moment with JTA(Narayana) and CDI. It has a transactional issue discovered in https://developer.jboss.org/message/990406 and @TransactionalScoped works better at this moment.
  • Check that your logging configuration is working and set logging to debug level.

希望能对您有所帮助.

致谢.

这篇关于在CDI bean中使用Spring Data Repo时,启动挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆