Application context loads twice - Spring Download

I have a pretty simple setup. The maven project with three modules: core / webapp / model. I am using Spring boot to prepare my application. In webapp, I have a simple WebappConfig class as follows:

@Configuration @EnableAutoConfiguration @ComponentScan(excludeFilters = @ComponentScan.Filter(Configuration.class)) public class WebappConfig { public static void main(String[] args) { SpringApplication app = new SpringApplication(WebappConfig.class); app.setAdditionalProfiles("dev"); app.run(args); } } 

and several classes in the kernel / model module. My point-container-application:

 public class AbcdXml extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WebappConfig.class); } } 

And not web.xml! My pom model has the following Spring dependency related to loading:

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> 

Core pom.xml:

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> 

Now launching WebappConfig through the "Run as β†’ Java" application works fine, but I need to deploy the project as a war against tomcat7. Webapp packaging is war. There is no tomcat in lib except tomcat-jdbc and tomcat-tuli jar (shouldn't there be problems?).

When I deploy my abcd.war, applicationcontext loads twice and results in the following stracktrace error:

 2014-06-27 11:06:08.445 INFO 23467 --- [ost-startStop-1] oaccC[.[localhost].[/abcd] : Initializing Spring embedded WebApplicationContext 2014-06-27 11:06:08.446 INFO 23467 --- [ost-startStop-1] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 19046 ms 2014-06-27 11:06:21.308 INFO 23467 --- [ost-startStop-1] osbceServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2014-06-27 11:06:21.313 INFO 23467 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*] 2014-06-27 11:06:21.314 INFO 23467 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2014-06-27 11:06:26.073 INFO 23467 --- [ost-startStop-1] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' 2014-06-27 11:06:26.127 INFO 23467 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2014-06-27 11:06:26.511 INFO 23467 --- [ost-startStop-1] org.hibernate.Version : HHH000412: Hibernate Core {4.3.1.Final} 2014-06-27 11:06:26.521 INFO 23467 --- [ost-startStop-1] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2014-06-27 11:06:26.527 INFO 23467 --- [ost-startStop-1] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist //some info messages from spring boot 2014-06-27 11:07:31.664 INFO 23467 --- [ost-startStop-1] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-06-27 11:07:33.095 INFO 23467 --- [ost-startStop-1] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-06-27 11:07:33.096 INFO 23467 --- [ost-startStop-1] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-06-27 11:07:36.080 INFO 23467 --- [ost-startStop-1] osjeaAnnotationMBeanExporter : Registering beans for JMX exposure on startup 2014-06-27 11:08:49.583 INFO 23467 --- [ost-startStop-1] osboot.SpringApplication : Started application in 183.152 seconds (JVM running for 210.258) 2014-06-27 11:12:29.229 ERROR 23467 --- [ost-startStop-1] oaccC[.[localhost].[/abcd] : Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml! at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) 

There is no web.xml, as I mentioned earlier.

A few interesting things that I can’t understand why:

  • After the outbreak of war, tomcat somehow creates a ROOT folder with the default web.xml. [Must be Spring incorrect boot configuration. How can i fix this? Pointers please?]
  • Even if I return to the same "application" SpringApplicationBuilder in AbcdXml.java, I am faced with the same problem as the contents of the application downloaded twice.

Thank you for your help!

EDIT 1:

The content of web.xml generated in the ROOT folder:

 <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5"> </web-app> 
+6
source share
4 answers

If your application includes jersey-spring3 and you are not taking steps to disable it, it will try to create an ApplicationContext for you (useful, not). There is a way to disable it (in WebApplicationInitializer ):

 servletContext.setInitParameter("contextConfigLocation", "<NONE>"); 

Or just use this: https://github.com/dsyer/spring-boot-jersey (specify as a dependency).

+13
source

In my case, the culprit used Spring Boot 1.3.0.M4 along with Jersey 2.21. When I downgraded Spring Boot to 1.2.6.RELEASE the problem disappeared. The only thing I needed to do was explicitly override the following properties, since I needed Spring 4.2.0 to support Hibernate 5, and jackson 2.6.2 support for JSR310 (java8 java.time):

 <spring.version>4.2.0.RELEASE</spring.version> <jackson.version>2.6.2</jackson.version> 

EDIT: with spring-boot 1.3.0.RELEASE, this error still exists. See github

+1
source

In my case - I had the same problem - to see the Spring splash screen twice - this was because I had 2 classes that extended SpringBootServletInitializer . One of them was called SpringBootWebApplication , and the other was ServletInitializer . I just uninstalled ServletInitializer and it worked fine. I don’t know why there were 2 classes - perhaps because I got inspiration from 2 different examples to collect what I need.

+1
source

@EnableAutoConfiuguration from the API says: "Turn on the autoconfiguration of the Spring application context, trying to guess and configure the beans that you probably need."

@ComponentScan will also create an instance of beans. IT scans packets, finds and registers beans.

How do you use both of these annotations, I think, therefore downloading it twice.

-1
source

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


All Articles