springboot打成war包重复启动两次

springboot打成war包配置流程

1、在启动类上继承SpringBootServletInitializer

public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Application.class);
    }
}

2、在pom上去掉springboot内嵌的tomcat

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                    <groupId>org.springframework.bootgroupId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providescope>
        dependency>

3、在pom查看是否有springboot的打包插件(这个就是引起启动两次的原因)

    <build>
        <finalName>mesfinalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

注意: 启动两次有两个原因:
1、war包的原因,一般按照上面没有问题;
2、可能是tomcat配置原因,最好用一个新的tomcat测一下,如果是tomcat原因,看一下server.xml的配置。按照如下配置,一般没问题:

 <Service name="Catalina"> 
    <Connector port="8011" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8012" protocol="AJP/1.3" redirectPort="8443"  secretRequired="false"/>
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      Realm>
      <Host name="localhost"  appBase="webapps_tinyid"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
      Host>
    Engine>
  Service> 

你可能感兴趣的:(spring,boot,tomcat,java)