springboot war package is deployed to tomcat to run

1. Modify the pom.xml file
insert image description here

2. Modify the SpringBoot startup class
springboot war package needs to inherit the SpringBootServletInitializer class to rewrite the configure method

package ths.project.portal;

import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import ths.project.portal.solid.util.InitUtils;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableScheduling
@Configuration
@EnableCasClient // 开启CAS支持
@ImportResource(locations= {"classpath:conf/spring.xml"})
public class DatacenterSlzyApplication extends SpringBootServletInitializer {
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new InitUtils(), "/socket");
    }

    public static void main(String[] args) {
        SpringApplication.run(DatacenterSlzyApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DatacenterSlzyApplication.class);
    }


}

There are a few things to note:

1. The packaging method in the jar package can be modified according to your own needs

2. If packaged into a war package, you need to inherit the org.springframework.boot.context.web.SpringBootServletInitializer class and override its config(SpringApplicationBuilder) method

3. If it is packaged into war, if there is no web.xml file in the packaged file, you can add the simplest web.xml (only the definition of the root node, but no sub-elements) to prevent the lack of web.xml file while the deployment fails with

**

After the above configuration, you need to check the following parts

The spring boot project comes with tomcat, but sometimes we need to package the project into a war and run it in an independent tomcat. At this time, we need to exclude the tomcat that comes with it. At this time, we can To use provided, we write this in the pom file:

        <!-- tomcat 的支持,打war包时去除springboot内嵌tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
             <scope>provided</scope>
        </dependency>

Several attributes and meanings of the scope tag:

1.compile: The default value means that the dependent project needs to participate in the compilation of the current project, as well as subsequent tests, and the operation cycle is also involved, which is a relatively strong dependency. It usually needs to be included when packaging.

2.test: Dependent projects only participate in test-related work, including compilation and execution of test code, and will not be packaged, such as junit.

3. runtime: Indicates that the dependent project does not need to participate in the compilation of the project, but its participation is required for later testing and running cycles. Compared with compile, the compilation is skipped. Such as JDBC driver, suitable for running and testing phase.

4. Provided: You don’t need to include it when packing, other facilities will be provided. In fact, the dependency can theoretically participate in the compilation, testing, running and other cycles. It is equivalent to compile, but the exclude operation is done in the packaging phase.

5.system: In terms of participation, it is the same as provided, but the dependent items will not be downloaded from the maven repository, but taken from the local file system. The property of systemPath needs to be added to define the path.

Guess you like

Origin blog.csdn.net/weixin_41377835/article/details/90673523