spring boot 如何设置为不为web方式启动

package com.cloudtech.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;

import com.cloudtech.config.NettyConfig;
import com.cloudtech.server.Server;
import com.cloudtech.util.Consts;

@SpringBootApplication
@ComponentScan(value = {"com.cloudtech"})
public class Aws310Application implements CommandLineRunner {
	protected static final Logger LOGGER = LoggerFactory.getLogger(Aws310Application.class);
	
	@Autowired
	NettyConfig nettyConfig;
	
	public static void main(String[] args) {
		new SpringApplicationBuilder(Aws310Application.class)
	    .web(WebApplicationType.NONE)
	    .run(args);
		//SpringApplication.run(Aws310Application.class, args);
	}
    
	/**
	 * spring boot启动后,会进入该方法
	 */
	@Override
	public void run(String... args) throws Exception {
		init();
	}

	/**
	 * 初始化信息
	 */
	@SuppressWarnings("unused")
	private void init() {
		Consts.SERVER_PORT = nettyConfig.getPort();
		Consts.MODULE_TYPE = nettyConfig.getModuleType();
	}
}

可以通过main里面这样设置

这里代码实现了项目一启动就会初始化加载run()方法,用来做一些配置的读取

 

欢迎关注公众号,免费领取各种学习视频,学习相关java各个技术点面试知识,有相关学习疑问或者面试困惑也可以私信博主

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值