使用spring-loaded开源项目,实现java程序和web应用的热部署
JDK1.5之后提供了java.lang.instrument.Instrumentation,即java agent机制能够实现类的redefinition和retransform。
redefinition对应Instrumentation.redefineClasses()能够实现类的热替换,但遗憾的是功能很有限。
The redefinition may change method bodies, the constant pool and attributes. The redefinition must not add, remove or rename fields or methods, change the signatures of methods, or change inheritance. These restrictions maybe be lifted in future versions.
Spring Loaded is a JVM agent for reloading class file changes whilst a JVM is running. It transforms classes at loadtime to make them amenable to later reloading. Unlike 'hot code replace' which only allows simple changes once a JVM is running (e.g. changes to method bodies), Spring Loaded allows you to add/modify/delete methods/fields/constructors. The annotations on types/methods/fields/constructors can also be modified and it is possible to add/remove/change values in enum types.
package test; import demo.A; public class TestPreMain { // -javaagent:springloaded-1.2.0.RELEASE.jar -noverify public static void main(String[] args) throws Exception { A a = new A(); while (true) { a.say(); Thread.sleep(3000); } } }
-javaagent:springloaded-1.2.0.RELEASE.jar -noverify
set JAVA_OPTS=-javaagent:springloaded-1.2.0.RELEASE.jar -noverify
这样就完成了spring-loaded的安装,能够检测tomcat下部署的webapp,在不重启tomcat的情况下,实现应用的热部署。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。