我們可以通過繼承SpringBoot的SpringApplication類來自定義SpringBoot的main方法。下面是一個示例代碼:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class CustomSpringBootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CustomSpringBootApplication.class);
}
public static void main(String[] args) {
SpringApplication application = new SpringApplication(CustomSpringBootApplication.class);
application.setBannerMode(Banner.Mode.OFF); // 關閉啟動時的Banner
application.run(args);
}
}
在這個示例中,我們創建了一個CustomSpringBootApplication類,繼承了SpringBoot的SpringBootServletInitializer類。我們重寫了configure方法,用來指定SpringBoot應用的啟動類。在main方法中,我們創建了一個SpringApplication對象,并通過setBannerMode方法關閉了啟動時的Banner,然后調用run方法來啟動SpringBoot應用。通過這種方式,我們可以自定義SpringBoot的main方法。