打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用spring
userphoto

2015.06.16

关注

JDK1.5之后提供了java.lang.instrument.Instrumentation,即java agent机制能够实现类的redefinition和retransform。

redefinition对应Instrumentation.redefineClasses()能够实现类的热替换,但遗憾的是功能很有限。

  1. The redefinition may change method bodies, the constant pool and attributes.  
  2. The redefinition must not add, remove or rename fields or methods, change the   
  3. signatures of methods, or change inheritance.  These restrictions maybe be  
  4. lifted in future versions.    

最近遇到一个开源项目spring-loaded,看了下官方的介绍文档:发现它功能比JDK自带的强大多了。
  1. Spring Loaded is a JVM agent for reloading class file changes whilst a JVM is running.   
  2. It transforms classes at loadtime to make them amenable to later reloading.   
  3. Unlike 'hot code replace' which only allows simple changes once a JVM is running   
  4. (e.g. changes to method bodies), Spring Loaded allows you to   
  5. add/modify/delete methods/fields/constructors.   
  6. The annotations on types/methods/fields/constructors   
  7. can also be modified and it is possible to add/remove/change values in enum types.  

经过自己的尝试,发现使用spring-loaded项目,确实可以实现java应用的热部署。下面介绍下如何将spring-loaded引入到项目中。我们可以运行下面的这段代码,然后修改A.say()方法,看看在不重启JVM的情况下,是否能够动态改变。
  1. package test;  
  2.   
  3. import demo.A;  
  4.   
  5. public class TestPreMain  
  6. {  
  7.   
  8.     // -javaagent:springloaded-1.2.0.RELEASE.jar -noverify  
  9.     public static void main(String[] args) throws Exception  
  10.     {  
  11.   
  12.         A a = new A();  
  13.   
  14.         while (true)  
  15.         {  
  16.             a.say();  
  17.             Thread.sleep(3000);  
  18.         }  
  19.     }  
  20. }  

为了使用spring-loaded实现热部署,我们只需要在启动JVM的时候,增加如下的启动参数即可
  1. -javaagent:springloaded-1.2.0.RELEASE.jar -noverify  

如果是通过eclipse启动,那么可以在run confiuration中进行设置



接下来我们看下如何在tomcat中使用spring-loaded实现war包的热部署。将下载的springloaded-1.2.0.RELEASE.jar放到%TOMCAT_HOME%/bin/目录下,然后修改该目录下的catalina.bat
  1. set JAVA_OPTS=-javaagent:springloaded-1.2.0.RELEASE.jar -noverify  

这样就完成了spring-loaded的安装,能够检测tomcat下部署的webapp,在不重启tomcat的情况下,实现应用的热部署。



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Drury & Son
跟老外用英语怎样介绍中国新年习俗?
Netweaver的端口号和Spring boot内嵌的Tomcat端口
How to successfully write a thesis.pdf
sciter-basics
Passing Arguments to Constructors During Load
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服