Article From:https://segmentfault.com/q/1010000011120247
Question:

Abnormity description

My development environment is as follows

Frame: springboot, mybatis…

Tool: Eclipse

Database: MySQL

In the development project, startup with boot class can start and test normally. The problem is that I put the project into war package, and the whole project can not start.

I did the following when I was packing:1

1.Modify the boot class

Inherit the boot classSpringBootServletInitializer,And rewroteconfigureMethod.

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  // Note that we want to point to the Application boot class that was originally implemented in main.Return builder.sources (Application.class);}

2.Modify dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- Remove the embedded Tomcat plug-in -->< exclusions>< exclusion>< groupId> ORG.springframework.boot< /groupId>< artifactId> spring-boot-starter-tomcat< /aRtifactId>< /exclusion>< /exclusions>< /dependency><! -- add servlEt-api's dependence on -->< dependency>< groupId> org.apache.tomcat< /groupId>< artIfactId> tomcat-servlet-api< /artifactId>< version> 8.0.36< /version>< sCope> provided< /scope>< /dependency>

3.Modification of packaging

<packaging>war</packaging>

4.maven-installPack

After packing, it was executed in tomcat-8.0.46. The following error occurred.

13-Sep-2017 09:49:34.794 Serious [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal One or more FilterS failed to start. Full details will be found in the in13-Sep-2017 09:49:34.799 serious [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [/cloudwifi] startup failed due to previous errors[localhost-startStop-1] INFO o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Closing org.springframework.boot.context.embedded.AnnotationCoNfigEmbeddedWebApplicationContext@1fb1bdfc: startup date [Wed Sep 13 09:49:21 CST 2017] root of conText hierarchy[localhost-startStop-1] INFO o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-eXposed beans on shutdown[localhost-startStop-1] INFO o.s.o.j.LocalContainerEntityManagerFactoryBeaN - Closing JPA EntityManagerFactory for persistence unit'default'13-Sep-2017 09:49:34.857 warns [locAlhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web appLication [cloudwifi] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister toWhen the web application was stopped. To prevent a memory leakRegistered.13-Sep-2017 09:49:34.860 warns [localhost-startStop-1] org.apache.catalina.loader.WebappClaSsLoaderBase.clearReferencesThreads The web application [cloudwifi] appears to have started a threadNamed [Abandoned connection cleanup thread] but has failed to stop it. stopE a memory leak. Stack trace of thread:Java.lang.Object.wait (Native Method)

Then the following error prompts appear in the localhost.log log.

13-Sep-2017 09:49:19.318 Information [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInItializers detected on classpath13-Sep-2017 09:49:25.494 information [localhost-startStop-1] org.apache.cataLina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext13-Sep-2017 09:49:34.793 serious [localhost-startStop-1] org.apache.catalina.core.StandardContext.filterStart Exception sTarting filter com.some.filter.Filter003Javax.naming.NamingException: Cannot create resource instaNceAt org.apache.naming.factory.FactoryBase.getObjectInstance (FactoryBase.java:96)At javax.naming.spi.NamingManager.getObjectInstance (NamingManager.java:321)At org.apache.naming.NamingCOntext.lookup (NamingContext.java:847)At org.apache.naming.NamingContext.lookup (NamingContext.ja)Va:172)At org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource (DefaultInstanceMa)Nager.java:550)At org.apache.catalina.core.DefaultInstanceManager.processAnnotations (DefaultIns)TanceManager.java:452)At org.apache.catalina.core.DefaultInstanceManager.newInstance (DefaultIns)TanceManager.java:142)At org.apache.catalina.core.DefaultInstanceManager.newInstance (DefaultIns)TanceManager.java:119)At org.apache.catalina.core.ApplicationFilterConfig.getFilter (Application)FilterConfig.java:258)At org.apache.catalina.core.ApplicationFilterConfig.< init> (ApplicatIonFilterConfig.java:105)At org.apache.catalina.core.StandardContext.filterStart (StandardContex)T.java:4700)At org.apache.catalina.core.StandardContext.startInternal (StandardContext.java:5340))At org.apache.catalina.util.LifecycleBase.start (LifecycleBase.java:145)At org.apache.cataLina.core.ContainerBase.addChildInternal (ContainerBase.java:753)At org.apache.catalina.core.ConTainerBase.addChild (ContainerBase.java:729)At org.apache.catalina.core.StandardHost.addChild (St)AndardHost.java:717)At org.apache.catalina.startup.HostConfig.deployDirectory (HostConfig.java:1)129)At org.apache.catalina.startup.HostConfig$DeployDirectory.run (HostConfig.java:1871)AtJava.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511)At java.util.concurrent.FutureTask.run (FutureTask.java:266)At java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadP)OolExecutor.java:1142)At java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.)Java:617)At java.lang.Thread.run (Thread.java:745)13-Sep-2017 09:49:34.794 serious [localhost-startStop-1] org.apache.catalina.core.StandardContext.filterStart Exception starting filter com.some.filtEr.Filter002Javax.naming.NamingException: Cannot create resource instanceAt org.apache.naming.factory.FactoryBase.getObjectInstance (FactoryBase.java:96)At javax.naming.spi.NamingManager.geTObjectInstance (NamingManager.java:321)At org.apache.naming.NamingContext.lookup (NamingContext.)Java:847)At org.apache.naming.NamingContext.lookup (NamingContext.java:172)At org.apache.caTalina.core.DefaultInstanceManager.lookupFieldResource (DefaultInstanceManager.java:550)At org.aPache.catalina.core.DefaultInstanceManager.processAnnotations (DefaultInstanceManager.java:452)AT org.apache.catalina.core.DefaultInstanceManager.newInstance (DefaultInstanceManager.java:142)AT org.apache.catalina.core.DefaultInstanceManager.newInstance (DefaultInstanceManager.java:119)AT org.apache.catalina.core.ApplicationFilterConfig.getFilter (ApplicationFilterConfig.java:258)AT org.apache.catalina.core.ApplicationFilterConfig.< init> (ApplicationFilterConfig.java:105)At org.apache.catalina.core.StandardContext.filterStart (StandardContext.java:4700)At org.apacHe.catalina.core.StandardContext.startInternal (StandardContext.java:5340)At org.apache.catalina.util.LifecycleBase.start (LifecycleBase.java:145)At org.apache.catalina.core.ContainerBase.addCHildInternal (ContainerBase.java:753)At org.apache.catalina.core.ContainerBase.addChild (Containe)RBase.java:729)At org.apache.catalina.core.StandardHost.addChild (StandardHost.java:717)AtOrg.apache.catalina.startup.HostConfig.deployDirectory (HostConfig.java:1129)At org.apache.catalIna.startup.HostConfig$DeployDirectory.run (HostConfig.java:1871)At java.util.concurrent.ExecutoRs$RunnableAdapter.call (Executors.java:511)At java.util.concurrent.FutureTask.run (FutureTask.ja)Va:266)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)13-Sep-2017 09:49:34.807 information [localhost-startStop-1] org.apache.catalina.Core.ApplicationContext.log Closing Spring root WebApplicationContext

The above is a detailed process of error


  1. 1 ↩
Answer 0:

Anomaly location

Note an exception message in the console when Tomcat starts.

One or more Filters failed to start. 

In other words, Tomcat was wrong when loading Filter. Check my Filter, I found the following dependency

@Resource(name = "someone")
private SomeObject someone;

The specific classes are hidden here, but SomeObject has the following characteristics.

1.He is a dependency on Spring

2.His specific value is dependent on the database

Question 1

Troubleshooting is always a problem of startup sequence, but it’s not very specific. Review the loading sequence first

listener–>filter–>sevlet–>spring

Because my filter relies on objects in spring, in fact, filter will load filter exceptions during loading.

Question 2

Use@ResourceAnnotation, this annotation is javax standard, in fact not only spring will use it, even Tomcat will use it. So Tomcat will try to load the resource someone when it starts. And because someone actually depends on the database,Only to appearjavax.naming.NamingException: Cannot create resource instanceThis exception.

Terms of settlement

Add Filter in other ways

First, Filter is no longer used@WebFilterAnnotation@ComponentAnnotate, turn this class into a component of Spring, and postpone its loading.

@Component
/*@WebFilter("/*")*/
public class Filter001
……

Do not use@Resourceannotation

Because Tomcat also recognizes@Resource,So we don’t use it instead of using it to use it.@Autowiredand@QualifierThese two annotations.

Adding Filter in the way of Spring

Just take a tutorial here.

http://blog.csdn.net/mzh1992/…

Answer 1:

ApplicationThe class is to inherit the SpringBootServletInitializer.

Leave a Reply

Your email address will not be published. Required fields are marked *