问题
I've got a REST service spring boot application, consisting into two separate maven projects:
- 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). - 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.
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.).
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.
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 maven dependency, containing Entity and Repository classes definitions.
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).
回答1:
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.
Apparently the problem is related with Spring Security an the custom UserDetailsService.
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.
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.
I don't know why it only happens when you deploy the application into a webserver (Glassfish 4.1.1 for me).
I hope it helps.
Other references:
https://github.com/spring-projects/spring-boot/issues/1008
回答2:
not sure if this will help anyone but...
I had the same issue and it wound up being a class that
extends org.springframework.boot.context.web.SpringBootServletInitializer
that was causing the issue. I was able to refactor the code and take that out, eliminating the error.
Hope someone finds this useful.
来源:https://stackoverflow.com/questions/38609791/spring-data-rest-unable-to-configure-localcontainerentitymanagerfactorybean-fr