Spring Data Rest: "Unable to configure LocalContainerEntityManagerFactoryBean from @EntityScan"

I have a REST service spring download application consisting of two separate maven projects:

  • The first myapp-data includes spring Data JPA + spring Definitions of the data repository and repository classes (I highlighted them in a separate maven project, since I use them also in other applications except the REST service).
  • secon myapp myapp-services is a spring boot application that basically contains the main configuration of the + method to expose the above spring Data Rest data repositories as REST endpoints.

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

Before some code changes that I made (in previous versions, for example, I used regular JPA repositories and custom stop controllers instead of spring Data Rest repositories), the application was deployed both in the development environment and in the intermediate environment. The problem probably arose when I added the spring Data Rest repositories, but I'm not 100% sure of this since I deployed in an intermediate environment after a number of other minor changes to the row.

This is the exception stack trace:

 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) 

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); } } 

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

In my service project configuration, I use spring Security with a custom UserDetailsService (I don’t know if this might be relevant, .. it worked in previous deployments, so this is probably not the case).

+3
source share
2 answers

I don’t know if it’s too late, but I will tell you what I did to fix this problem for future links.

The problem seems to be related to Spring Security - the custom UserDetailsService.

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

To fix this, you can add the @Order (1) tag to the main class and another (e.g. @Order (30)) to the security class.

I do not know why this only happens when you deploy the application to a web server (Glassfish 4.1.1 for me).

Hope this helps.

Other links:

https://github.com/spring-projects/spring-boot/issues/1008

+2
source

not sure if this will help anyone, but ...

I had the same problem and it turned out to be a class that

extends org.springframework.boot.context.web.SpringBootServletInitializer

what caused the problem. I was able to reorganize the code and remove it, fixing the error.

Hope someone finds this helpful.

+1
source

Source: https://habr.com/ru/post/1206140/


All Articles