📚 开源资源分享——颜佐光的个人书架
✉️ 联系站长
书架 / Java / SpringBoot

SpringBoot 如何设置为不为web方式启动

2019-04-28 18:25 · 约 1,446 字 · 浏览 3,411 次 · 更新 2020-05-15 15:22SpringBoot
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》目录书架 →