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()方法,用来做一些配置的读取

 

如果你热衷技术,喜欢交流,欢迎加入我们!

spring boot 如何设置为不为web方式启动_第1张图片

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