1.简介
Spring-Loaded是基于javaAgent实现的一个代理,更多javaAgent可以参考:java代理-javassist:
官网:
当前的最新版:1.2.4.RELEASE2.说明
目前最新版1.2.4.RELEASE实现了监听jar包的更新的机制,实现热更新jar包,目前官方提供的版本在linux上可以很好的运行,但在windows还存在bug,官网已经有人提出:。如果想在windows上测试可以自己手动改一下源码。
3.参数
-Dspringloaded=watchJars=main.jar:other.jar
即watchJars选项,监听的多个jar之前用:分离4.实例
以win为例,这里需要手动改一下源码,编译一下测试目录: lib:springloaded-1.2.4.RELEASE.jar main-0.0.1-SNAPSHOT.jar是测试的jar包,一般都是游戏逻辑jar包,测试
package org.main;import java.util.concurrent.TimeUnit;/** * Hello world! * */public class App { public static void main(String[] args) throws InterruptedException { Reload reload = new Reload(); while (true) { reload.load(); TimeUnit.SECONDS.sleep(2); } }}
package org.main;public class Reload { public void load() { System.out.println("load....."); }}run.bat:
java -javaagent:lib/springloaded-1.2.4.RELEASE.jar -noverify -Dspringloaded=verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jar -jar main-0.0.1-SNAPSHOT.jarpause运行run.bat
D:\springloaded\win>java -javaagent:lib/springloaded-1.2.4.RELEASE.jar -noverify -Dspringloaded=verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jar -jar main-0.0.1-SNAPSHOT.jarSL: [verbose mode on] Full configuration is:verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jarSL: [explain mode on] Reporting on the decision making process within SpringLoaded2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPreProcess信息: SpringLoaded preprocessing: classname=org/main/App classloader=Launcher$AppClassLoader typeRegistry=TypeRegistry(id=1325628529,loader=sun.misc.Launcher$AppClassLoader)====remainingPrefix=====jar:file:/D:/springloaded/win/main-0.0.1-SNAPSHOT.jar====jarname=====main-0.0.1-SNAPSHOT.jar2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor preProcess信息: [explanation] Based on the name, type org/main/App is considered to be reloadable2015-9-16 16:55:32 org.springsource.loaded.ReloadableType信息: New reloadable type: org.main.App (allocatedId=0) TypeRegistry(id=1325628529,loader=sun.misc.Launcher$AppClassLoader)2015-9-16 16:55:32 org.springsource.loaded.agent.Watcher addFile信息: Now watching d:\springloaded\win\main-0.0.1-SNAPSHOT.jar2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType信息: >TypeRegistry.getReloadableType(typeRegistryId=0,typeId=0)2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType信息: 信息: New reloadable type: org.main.Reload (allocatedId=1) TypeRegistry(id=1325628529,loader=sun.misc.Launcher$AppClassLoader)2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType信息: >TypeRegistry.getReloadableType(typeRegistryId=0,typeId=1)2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType信息:
修改jar中的类文件,将Reload中的load....改成reload.... 重新打包,并覆盖当前的jar,观察日志:
2015-9-16 16:57:32 org.springsource.loaded.agent.Watcher run信息: Observed last modification time change for d:\springloaded\win\main-0.0.1-SNAPSHOT.jar (lastScanTime=1442393851501)2015-9-16 16:57:32 org.springsource.loaded.agent.Watcher ......省略reload.....reload.....
热更新完成!
参考: