0.修改pom打包方式为war,同时排除了内置的tomcat。
- <packaging>war</packaging>
- <!-- 排除内置的tomcat -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>compile</scope>
- </dependency>
- <!-- 若直接有使用servlet对象时(这是废话,⊙﹏⊙‖∣),需要将servlet引入,本例是没有的~ -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <scope>provided</scope>
- </dependency>
1.改造下启动类,使其继承SpringBootServletInitializer,同时覆盖configure方法。
- @SpringBootApplication
- @Slf4j
- public class ChapterApplication extends SpringBootServletInitializer{
- public static void main(String[] args) {
- SpringApplication.run(ChapterApplication.class, args);
- // new SpringApplicationBuilder().sources(ChapterApplication.class).web(false).run(args);
- //之后这里设置业务逻辑 比如挂起一个线程 或者设置一个定时任务。保证不退出
- //不然它就是一个启动类,启动后就停止了。
- log.info("jar,chapter启动!");
- }
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- log.info("外部tomcat,chapter启动!");
- return application.sources(ChapterApplication.class);
- }
- }
2.maven打包成war(mvn clean install),然后放入tomcat中,启动运行即可。
其实这样设置的话,在开发时直接运行启动类也还是可以直接运行的,方便.
启动不设置端口
对一些定时任务服务项目,其本身只是提供一个定时调度功能,不需要其他服务调用,只是去调度其他服务。像这样的服务,正常也就不需要设置端口了。这时候SpringBoot也是支持的。只需要改下启动方式:
new SpringApplicationBuilder().sources(ChapterApplication.class).web(false).run(args);
//之后这里设置业务逻辑 比如挂起一个线程 或者设置一个定时任务。保证不退出
//不然它就是一个启动类,启动后就停止了。
或者修改配置文件的属性:
- spring.main.web-environment=false
最后效果,是不是没有看见端口了:
启动完成前进行业务逻辑
利用CommandLineRunner或者ApplicationRunner可实现在SpringApplication的run()完成前执行一些业务逻辑
0.修改启动类,实现CommandLineRunner接口,ApplicationRunner类似,只是run的入参不同而已。
- @Override
-
- public void run(String... args) throws Exception {
-
- log.info("CommandLineRunner运行");
-
- }
1.运行应用,注意查看控制台输出:
当然,直接申明一个bean也是可以的。
- @Configuration
-
- @Slf4j
-
- public class CommandLineRunnerConfig {
-
- @Bean
-
- public CommandLineRunner runner(){
-
- return new CommandLineRunner() {
-
- public void run(String... args){
-
- log.info("CommandLineRunner运行2");
-
- }
-
- };
-
- }
-
- }
若多个时,可设置@Order来确定执行的顺序。
动态修改日志级别
通过org.springframework.boot.logging.LoggingSystem提供的api即可。
loggingSystem.setLogLevel(null, LogLevel.DEBUG);
如,默认时是info模式,未修改时,debug模式是不会输出的。
动态设置后
热部署 (编辑:好传媒网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|