package cn.com.tenlion; import cn.com.tenlion.startup.AppStart; import cn.com.tenlion.util.AppUtil; import cn.com.tenlion.util.PortChecker; import org.h2.tools.Server; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.File; import java.util.HashMap; import java.util.Map; @SpringBootApplication(scanBasePackages = {"cn.com.tenlion"}) @MapperScan("cn.com.tenlion.mapper") public class AimzProjectApplication { public static void main(String[] args) { for (; ; ) { boolean isPortInUse = PortChecker.isPortInUse(AppStart.SERVER_PORT); if (!isPortInUse) { break; } AppStart.SERVER_PORT += 10; } System.out.println(AppStart.SERVER_PORT); startH2Server(); Map defaultProp = new HashMap<>(); defaultProp.put("server.port", AppStart.SERVER_PORT); defaultProp.put("spring.datasource.url", "jdbc:h2:tcp://localhost:" + (AppStart.SERVER_PORT + 1) + "/aimzdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"); SpringApplication app = new SpringApplication(AimzProjectApplication.class); app.setDefaultProperties(defaultProp); app.run(args); } private static void startH2Server() { String dbBaseDir = AppUtil.getAppPath() + File.separator + "db"; try { Server h2Server = Server.createTcpServer( "-tcp", "-tcpPort", String.valueOf(AppStart.SERVER_PORT + 1), "-tcpAllowOthers", "-ifNotExists", "-baseDir", dbBaseDir.replace(" ", "\\ ") ).start(); System.out.println("H2 Server started on port " + h2Server.getPort() + ", path: " + dbBaseDir); } catch (Exception e) { e.printStackTrace(); } } }