我發現這個地方所以道歉,原來的主人,但我要創造我加彈簧引導裝載程序依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
<scope>provided</scope>
</dependency>
因爲需要延長JarLauncher類的項目。 Spring引導提供了一個特殊的啓動器,它改變了java行爲類加載器。 org.springframework.boot.loader.JarLauncher類創建一個特殊的類加載器並增強應用程序。
由於我想將應用程序作爲窗口服務啓動,因此我選擇了Procrun作爲服務管理器。 Procrun需要兩個啓動和停止方法或一個帶有字符串數組參數的方法(有關更多詳細信息,請參閱procrun項目)。因此我創建了一個Bootsrap類,它擴展了JarLauncher並實現了Procrun需要的方法。
public class Bootstrap extends JarLauncher {
private static ClassLoader classLoader = null;
private static Bootstrap bootstrap = null;
protected void launch(String[] args, String mainClass, ClassLoader classLoader, boolean wait)
throws Exception {
Runnable runner = createMainMethodRunner(mainClass, args, classLoader);
Thread runnerThread = new Thread(runner);
runnerThread.setContextClassLoader(classLoader);
runnerThread.setName(Thread.currentThread().getName());
runnerThread.start();
if (wait == true) {
runnerThread.join();
}
}
public static void start (String []args) {
bootstrap = new Bootstrap();
try {
JarFile.registerUrlProtocolHandler();
classLoader = bootstrap.createClassLoader(bootstrap.getClassPathArchives());
bootstrap.launch(args, bootstrap.getMainClass(), classLoader, true);
}
catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
public static void stop (String []args) {
try {
if (bootstrap != null) {
bootstrap.launch(args, bootstrap.getMainClass(), classLoader, true);
bootstrap = null;
classLoader = null;
}
}
catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
String mode = args != null && args.length > 0 ? args[0] : null;
if ("start".equals(mode)) {
Bootstrap.start(args);
}
else if ("stop".equals(mode)) {
Bootstrap.stop(args);
}
}
}
在彈簧引導應用I類與改變的主要方法:
private static ApplicationContext applicationContext = null;
public static void main(String[] args) {
String mode = args != null && args.length > 0 ? args[0] : null;
if (logger.isDebugEnabled()) {
logger.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() +
" Application mode:" + mode + " context:" + applicationContext);
}
if (applicationContext != null && mode != null && "stop".equals(mode)) {
System.exit(SpringApplication.exit(applicationContext, new ExitCodeGenerator() {
@Override
public int getExitCode() {
return 0;
}
}));
}
else {
SpringApplication app = new SpringApplication(TestProcrunApplication.class);
applicationContext = app.run(args);
if (logger.isDebugEnabled()) {
logger.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() +
" Application started context:" + applicationContext);
}
}
}
然後,我安裝有prunsrv.exe
服務:
prunsrv.exe // // IS test-procrun --DisplayName =「test-procrun」--Description =「test-procrun」--Startup = auto --Install =%CD%\ prunsrv.exe --Jvm = auto --Classpath =%CD%。 。\ target \ test-procrun-0.0.1-SNAPSHOT.jar --StartMode = jvm --StartClass = it.test.procrun.Bootstrap - -StartMethod = start --StartParams = start --StopMode = jvm --StopClass = it.test.procrun.Bootstrap --StopMethod = stop --StopParams = stop --StdOutput = auto --StdError = auto --LogPath =% CD%--LogLevel =調試
謝謝大家的幫助!順便說一句,對不起,我忘記提及我正在Linux環境中尋找守護進程服務,特別是CentOS 7.你能給我任何建議嗎? –
認爲你得到這個來自:http://zazos79.blogspot.com/2015/02/spring-boot-12-run-as-windows-service.html,hth -adym – lincolnadym