Springboot项目部署在外置tomcat上,Application启动类是怎么没启动

启动类

@SpringBootApplication
public class MypracticeApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(MypracticeApplication.class, args);
        applicationContext.getBean(SocketServer.class).start();
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MypracticeApplication.class);
    }
}

@Service
@Slf4j
public class SocketServer {
    public void start() {
        log.info("=========");
    }
}

pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>mypractice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>mypractice</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- lombok插件 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>

        <!-- websocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

        <!-- spring boot集成redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <!-- 实现多个端口 -->
        <!--<dependency>-->
        <!--<groupId>commons-lang</groupId>-->
        <!--<artifactId>commons-lang</artifactId>-->
        <!--</dependency>-->

        <dependency>
            <groupId>org.java-websocket</groupId>
            <artifactId>Java-WebSocket</artifactId>
            <version>1.4.0</version>
        </dependency>

        <!-- 阿里云sdk -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>

        <!-- pagehelper分页工具 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

        <!--极光推送 -->
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.1.7</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

        <!-- junit jupiter -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.2</version>
            <!--此处test 是关键,表明底层日志实现框架-也即该依赖在test 代码编译、运行时才起作用-->
            <scope>test</scope>
        </dependency>

        <!--junit 5 需要参数化测试-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

问题:项目使用Springboot内置tomcat,在本地跑项目的时候SocketServer可以运行打印,但是部署在外置tomcat上就不打印了。为什么?

仔细看Tomcat的启动日志,有加载你的项目吗?加载过程有异常吗?
springboot以war包部署,还需要移除自带的Tomcat

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

嗯嗯,有加载,我tomcat就放了一个项目;异常也没有;现在加上了tomcat,但是还是没有打印,这是外置tomcat 部署的日志

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.1.RELEASE)

2019-12-03 15:18:45.825  INFO 8256 --- [ost-startStop-1] c.e.mypractice.MypracticeApplication     : Starting MypracticeApplication v0.0.1-SNAPSHOT on zhangxiaohuideMacBook-Pro.local with PID 8256 (/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/mypractice-0.0.1-SNAPSHOT/WEB-INF/classes started by zhangxiaohui in /usr/local/Cellar/tomcat@8/8.5.33/libexec/bin)
2019-12-03 15:18:45.830  INFO 8256 --- [ost-startStop-1] c.e.mypractice.MypracticeApplication     : No active profile set, falling back to default profiles: default
2019-12-03 15:18:46.844  INFO 8256 --- [ost-startStop-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-12-03 15:18:46.851  INFO 8256 --- [ost-startStop-1] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-12-03 15:18:46.894  INFO 8256 --- [ost-startStop-1] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11ms. Found 0 repository interfaces.
2019-12-03 15:18:47.100  WARN 8256 --- [ost-startStop-1] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.mypractice]' package. Please check your configuration.
2019-12-03 15:18:47.498  INFO 8256 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-03 15:18:47.686  INFO 8256 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1820 ms
2019-12-03 15:18:48.481  INFO 8256 --- [ost-startStop-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService
2019-12-03 15:18:48.486  INFO 8256 --- [ost-startStop-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientTaskPool'
2019-12-03 15:18:49.032  INFO 8256 --- [ost-startStop-1] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2019-12-03 15:18:49.166  INFO 8256 --- [ost-startStop-1] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2019-12-03 15:18:49.897  INFO 8256 --- [ost-startStop-1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2019-12-03 15:18:49.900  INFO 8256 --- [ost-startStop-1] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
2019-12-03 15:18:50.802  INFO 8256 --- [ost-startStop-1] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2019-12-03 15:18:50.821  INFO 8256 --- [ost-startStop-1] c.e.mypractice.MypracticeApplication     : Started MypracticeApplication in 5.823 seconds (JVM running for 11.514)
03-Dec-2019 15:18:50.863 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/mypractice-0.0.1-SNAPSHOT.war] has finished in [10,781] ms
03-Dec-2019 15:18:50.864 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/docs]
03-Dec-2019 15:18:50.887 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/docs] has finished in [22] ms
03-Dec-2019 15:18:50.887 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/manager]
03-Dec-2019 15:18:50.920 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/manager] has finished in [33] ms
03-Dec-2019 15:18:50.921 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/examples]
03-Dec-2019 15:18:51.131 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/examples] has finished in [211] ms
03-Dec-2019 15:18:51.131 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/ROOT]
03-Dec-2019 15:18:51.146 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/ROOT] has finished in [14] ms
03-Dec-2019 15:18:51.146 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/host-manager]
03-Dec-2019 15:18:51.164 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/Cellar/tomcat@8/8.5.33/libexec/webapps/host-manager] has finished in [18] ms
03-Dec-2019 15:18:51.168 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8085"]
03-Dec-2019 15:18:51.177 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
03-Dec-2019 15:18:51.179 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 11159 ms

这是本地启动启动类打印日志

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.1.RELEASE)

2019-12-03 15:23:37.345  INFO 8337 --- [           main] c.e.mypractice.MypracticeApplication     : Starting MypracticeApplication on zhangxiaohuideMacBook-Pro.local with PID 8337 (/Users/zhangxiaohui/IDEA-project/mypractice/target/classes started by zhangxiaohui in /Users/zhangxiaohui/IDEA-project/mypractice)
2019-12-03 15:23:37.348  INFO 8337 --- [           main] c.e.mypractice.MypracticeApplication     : No active profile set, falling back to default profiles: default
2019-12-03 15:23:38.214  INFO 8337 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-12-03 15:23:38.220  INFO 8337 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-12-03 15:23:38.251  INFO 8337 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 12ms. Found 0 repository interfaces.
2019-12-03 15:23:38.423  WARN 8337 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.mypractice]' package. Please check your configuration.
2019-12-03 15:23:38.717  INFO 8337 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-03 15:23:38.955  INFO 8337 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8083 (http)
2019-12-03 15:23:38.964  INFO 8337 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-12-03 15:23:38.965  INFO 8337 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-12-03 15:23:39.066  INFO 8337 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-12-03 15:23:39.066  INFO 8337 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1634 ms
2019-12-03 15:23:39.603  INFO 8337 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService
2019-12-03 15:23:39.611  INFO 8337 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientTaskPool'
2019-12-03 15:23:39.963  INFO 8337 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2019-12-03 15:23:40.028  INFO 8337 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2019-12-03 15:23:41.314  INFO 8337 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2019-12-03 15:23:41.315  INFO 8337 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
2019-12-03 15:23:42.216  INFO 8337 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2019-12-03 15:23:42.260  INFO 8337 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path ''
2019-12-03 15:23:42.264  INFO 8337 --- [           main] c.e.mypractice.MypracticeApplication     : Started MypracticeApplication in 5.658 seconds (JVM running for 6.897)
2019-12-03 15:23:42.266  INFO 8337 --- [           main] c.e.mypractice.threadpool.SocketServer   : =========

有打印

你把这个 SocketService 的启动放到监听器里面试试看。

监听器,可是没写监听器诶:sob:
为什么Springboot内置tomcat本地可以,放到外置tomcat就不行了

我现在把内置的移除了加了

org.springframework.boot spring-boot-starter-tomcat provided

打包到外置tomcat运行就是不打印
[/quote]

你新建一个包(spring可以扫描到的),把SocketService 定义在里面,添加 @Component 注解。不要直接定义在MypracticeApplication 中。然后再新建一个监听器,试试看。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class ApplicationStartListener implements ApplicationListener<ContextRefreshedEvent>{

	@Autowired
	private SocketService socketService;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		this.socketService.start();
	}
}

谢谢:pray:,解决了
一开始不明白为什么springboot内置tomcat启动的时候可以执行指定的方法,放到外置tomcat就不行类。:rofl:因为启动类在打成war后放置在外部容器tomcat上就不能依赖于Application的main函数了,不会执行,所以它不会执行添加在启动类里的方法:joy:。当然也可以打包成jar包这样就能使用main函数类,调用springboot内置tomcat。所以只能改变思路用监听的方式或者别的方式https://blog.csdn.net/limingcai168/article/details/92797258

3赞