springboot jar with a way to debug, deploy a war to tomcat way

Original link: https://dan326714.iteye.com/blog/2400168

Official link: http: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

 

The company developed using Springboot, but why do we still labeled as war package, it is because of our company's operation and maintenance done a hook, as long as the code to the master merge, it will automatically deploy to the corresponding tomcat below, so we have company with war bag deployment project, but tomcat is a very complete servlet container.

 

About playing war package Let's see how the official website says

The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container. Typically, you update your application’s main class to extend SpringBootServletInitializer:

 

Java code   Collection Code
@SpringBootApplication  
public class Application extends SpringBootServletInitializer {  
  
    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(Application.class);  
    }  
  
    public static void main(String[] args) throws Exception {  
        SpringApplication.run(Application.class, args);  
    }  
  
}  

 

 

 

The first step, balaba a lot, so that probably means that you inherit SpringBootServletInitializer on your main class, configure and implement methods.

 

The next step is to update your build configuration so that your project produces a war file rather than a jar file. If you’re using Maven and using spring-boot-starter-parent (which configures Maven’s war plugin for you) all you need to do is to modify pom.xml to change the packaging to war:

 

<packaging>war</packaging>

 

 

The second step will let you maven pom.xml are packaged into war in the package.

 

The final step in the process is to ensure that the embedded servlet container doesn’t interfere with the servlet container to which the war file will be deployed. To do so, you need to mark the embedded servlet container dependency as provided.

 

Java code   Collection Code
  1. <dependency>  
  2.         <groupId>org.springframework.boot</groupId>  
  3.         <artifactId>spring-boot-starter-tomcat</artifactId>  
  4.         <scope>provided</scope>  
  5. </dependency>  

 

The third part, let you join the jar package maven, and then you can feel at ease labeled as war package into the production environment.

 

In fact, there is the fourth step (official document did not give), write the name of war, finalName here.

 

 

Java code   Collection Code
  1. <build>  
            <finalName>websocket</finalName>  
            <plugins>  
                <plugin>  
                    <groupId>org.springframework.boot</groupId>  
                    <artifactId>spring-boot-maven-plugin</artifactId>  
                </plugin>  
            </plugins>  
    </build>  

 

Test production environment (http: // host: port / war package name), found no problem after the test, flattered work, and the next day you turn on this development project, you find that the project started on your idea is not up a. Times start the project such a mistake.

 

Java code   Collection Code
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@4386f16: startup date [Sat Nov 18 23:58:05 CST 2017]; root of context hierarchy  
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414)  
    at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97)  
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253)  
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)  
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)  
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961)  
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)  
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230)  
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968)  
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030)  
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556)  
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)  
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)  
    at com.easyto.websocket.WebsocketApplication.main(WebsocketApplication.java:11)  
2017-11-18 23:58:05.752 [ERROR] org.springframework.boot.SpringApplication - Application startup failed  
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.easyto.websocket.WebsocketApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer  
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)  
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)  
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)  
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270)  
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)  
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)  
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)  
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)  
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)  
    at com.easyto.websocket.WebsocketApplication.main(WebsocketApplication.java:11)  
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer  
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163)  
    at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:380)  
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:314)  
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)  
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198)  
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)  
    ... 12 common frames omitted  
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext  
    at java.lang.Class.getDeclaredMethods0(Native Method)  
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)  
    at java.lang.Class.getDeclaredMethods(Class.java:1975)  
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152)  
    ... 17 common frames omitted  
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext  
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)  
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)  
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)  
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)  
    ... 21 common frames omitted  

 

 

You would think this is what the hell? In fact, this document is the official website of the pit, to solve this problem, you need to range spring-boot-starter-tomcat package introduced in front of them, or comment out entire jar package

 

Java code   Collection Code
  1. <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-tomcat</artifactId>  
        <!--<scope>provided</scope>-->  
    </dependency>  

 

After the experiment, you can put the whole package are commented out.

 

Java code   Collection Code
  1. <!--<dependency>-->  
                <!--<groupId>org.springframework.boot</groupId>-->  
                <!--<artifactId>spring-boot-starter-tomcat</artifactId>-->  
                <!--<scope>provided</scope>-->  
    <!--</dependency>-->  

Then you can also put you start the class code commented out.

 

Java code   Collection Code
@Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(Application.class);  
    }  

 

 

Then you will find that the world is beautiful, the resulting package to run at tomcat, use springboot project under way in the idea also perfect run

 

Original ending.


 

 

Solutions to problems encountered: SpringBootServletInitializer follow the tutorial operation will complain, because different versions of the original author springboot, change it enough.

 

Guess you like

Origin www.cnblogs.com/CryOnMyShoulder/p/11281990.html