Spring Data Rest:“无法从@EntityScan 配置 LocalContainerEntityManagerFactoryBean"; [英] Spring Data Rest: "Unable to configure LocalContainerEntityManagerFactoryBean from @EntityScan"

查看:35
本文介绍了Spring Data Rest:“无法从@EntityScan 配置 LocalContainerEntityManagerFactoryBean";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 REST 服务 spring boot 应用程序,由两个独立的 maven 项目组成:

I've got a REST service spring boot application, consisting into two separate maven projects:

  1. 第一个 myapp-data 包括 Spring Data JPA + Spring Data Rest Entities 和 Rest Repositories 类定义(我已经将它们隔离在一个单独的 maven 项目中,因为我也在其他应用程序中使用它们我的 REST 服务).
  2. 第二个 myapp-services 是一个 Spring Boot 应用程序,它基本上包含主要方法 + 配置以将上述 Spring Data Rest 存储库公开为 REST 端点.
  1. The first one myapp-data includes Spring Data JPA + Spring Data Rest Entities and Rest Repositories class definitions (I've isolated these in a separate maven project since I use them also in other applications besides my REST service).
  2. The secon myapp-services is a Spring Boot application that basically contains main method + configuration to expose the above Spring Data Rest repositories as REST endpoints.

如果我在本地开发环境(STS Eclipse,在本地开发机器上启动 mvn spring-boot:run )运行它,一切似乎都正常,但是当我将它部署为战争时在暂存的 tomcat 环境(ubuntu 14.04 下的 tomcat7)中,我在下面的框中收到了异常.请注意,除此之外,两种部署的区别仅在于 application.properties 中的不同设置(数据库连接、日志记录级别等).

If I run this in a local development environment (STS Eclipse, launching mvn spring-boot:run on my local development machine) everything seems to work fine, but when I deploy it as a war on a staging tomcat environment (tomcat7 under ubuntu 14.04) I got the exception in the box below. Note that besides this the difference in the two deployments consists only in different settings of in application.properties (db connection, logging levels, etc.).

在我进行一些代码更改之前(例如,在以前的版本中,我使用普通 JPA 存储库和自定义 Rest 控制器而不是 Spring Data Rest 存储库),应用程序在开发和暂存环境中部署得很好.当我添加 Spring Data Rest 存储库时,问题可能开始出现,但我不是 100% 确定这一点,因为我在连续进行了一些其他小的更改后部署在临时环境中.

Before some code changes I made (in previous versions for instance I used normal JPA repositories and custom Rest Controllers instead of Spring Data Rest repositories) the application deployed fine both in development and staging environments. The problem probably started to arise when I added the Spring Data Rest repositories, but I'm not 100% sure of this since I deployed in the staging environment after a number of other minor changes in a row.

这是异常堆栈跟踪:

java.lang.IllegalStateException: Unable to configure LocalContainerEntityManagerFactoryBean from @EntityScan, ensure an appropriate bean is registered.
        at org.springframework.util.Assert.state(Assert.java:392)
        at org.springframework.boot.orm.jpa.EntityScanRegistrar$EntityScanBeanPostProcessor.afterSingletonsInstantiated(EntityScanRegistrar.java:148)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:792)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
        at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
        at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:149)
        at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:129)
        at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5456)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1256)
        at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:692)
        at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:217)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:213)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

这是 myapp-services 项目的主要应用程序类:

This is the main Application class for the myapp-services project:

package eu.myapp.services;

@ComponentScan(value="eu.myapp")
@EnableJpaRepositories("eu.myapp.data")
@EntityScan(value="eu.myapp.data")
@SpringBootApplication
public class MyappServicesApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MyappServicesApplication.class, args);
    }
}

其中 eu.myapp.data 是在 myapp-data 项目中定义的包,作为 maven 依赖包含,包含实体和存储库类定义.

where eu.myapp.data is the package defined in the myapp-data project included as maven dependency, containing Entity and Repository classes definitions.

在我的服务项目配置中,我使用带有自定义 UserDetailsS​​ervice 的 Spring Security(不知道这是否相关,..它在以前的部署中有效,所以可能不是).

In my service project configuration I use Spring Security with a custom UserDetailsService (don't know whether this could be relevant, .. it worked in previous deployments, so probably it is not).

推荐答案

我不知道是不是太晚了,但我会告诉你我做了什么来解决这个问题以供将来参考.

I don't know if it's too late but I'm going to tell you what I have done to fix this problem for future references.

显然问题与 Spring Security 和自定义 UserDetailsS​​ervice 有关.

Apparently the problem is related with Spring Security an the custom UserDetailsService.

我认为一旦他们尝试同时访问数据库层时,主应用程序类和您的SpringSecurity配置类之间存在冲突.

I think that there is a conflict between the main application class and your configuration class for SpringSecurity once they try to access to the database layer at the same time.

要解决此问题,您可以将标签@Order(1) 添加到主类,并将不同的标签(例如@Order(30))添加到安全类.

To fix this, you can add the tag @Order(1) to the main class and a different one (@Order(30) for example) to the security class.

我不知道为什么只有在您将应用程序部署到网络服务器时才会发生这种情况(对我来说是 Glassfish 4.1.1).

I don't know why it only happens when you deploy the application into a webserver (Glassfish 4.1.1 for me).

希望能帮到你.

其他参考:

https://github.com/spring-projects/spring-boot/问题/1008

这篇关于Spring Data Rest:“无法从@EntityScan 配置 LocalContainerEntityManagerFactoryBean";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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