<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>

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

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

1
2
3
4
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,看了下官方的介绍文档:发现它功能比JDK自带的强大多了。

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.

Spring Loaded is usable on any bytecode that may run on a JVM, and is actually the reloading system used in Grails 2.

https://github.com/spring-projects/spring-loaded

经过自己的尝试,发现使用spring-loaded项目,确实可以实现java应用的热部署。下面介绍下如何将spring-loaded引入到项目中。我们可以运行下面的这段代码,然后修改A.say()方法,看看在不重启JVM的情况下,是否能够动态改变。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
        }
    }
}

为了使用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的情况下,实现应用的热部署。

http://www.2cto.com/kf/201411/348927.html

当然,它也有一些小缺限: 
1. 目前官方提供的1.2.4 版本在linux上可以很好的运行,但在windows还存在bug,官网已经有人提出:https://github.com/spring-projects/spring-loaded/issues/145 
2. 对于一些第三方框架的注解的修改,不能自动加载,比如:spring mvc的@RequestMapping 
3. log4j的配置文件的修改不能即时生效。

http://blog.csdn.net/catoop/article/details/51034778

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>

Spring Loaded is a JVM agent for reloading class file changes的更多相关文章

  1. 使用Ratpack与Spring Boot构建高性能JVM微服务

    在微服务天堂中Ratpack和Spring Boot是天造地设的一对.它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率.效率以及轻量级部署.他们在服务程序的开发中带来了各自的好处.R ...

  2. 备战金九银十,Java研发面试题(Spring、MySQL、JVM、Mybatis、Redis、Tomcat)[带答案],刷起来!

    八月在即,马上就是"金九银十",又是跳槽招聘季.咱们这行公认涨薪不如跳槽加的快.但不建议频繁跳槽,还是要学会融合团队,抓住每个机会提升技能. 苏先生在这里给大家整理了一套各大互联网 ...

  3. tomcat使用spring-loaded实现应用热部署

    springloaded官方说明: Spring Loaded is a JVM agent for reloading class file changes whilst a JVM is runn ...

  4. 使用spring-loaded开源项目,实现java程序和web应用的热部署

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

  5. 翻译-使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中 ...

  6. 使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...

  7. JAVA热部署,通过agent进行代码增量热替换!!!

    在前说明:好久没有更新博客了,这一年在公司做了好多事情,包括代码分析和热部署替换等黑科技,一直没有时间来进行落地写出一些一文章来,甚是可惜,趁着中午睡觉的时间补一篇介绍性的文章吧. 首先热部署的场景是 ...

  8. JDWP Agent

    JDWP Agent Implementation Description Revision History Disclaimer 1. About this Document 1.1 Purpose ...

  9. 玩转spring boot——properties配置

    前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...

随机推荐

  1. Python中的List,Tuple,Dic,Set

    Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...

  2. 【Largest Rectangle in Histogram】cpp

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  3. OpenFramework中视频或者图片进行中心旋转、平移、放大、缩小、矫正(本例以视频为准,只给出主要代码)

    /********** update mesh部分***********/ for(int i=0;i<4;i++) {  mesh[i].clear(); //重要,不加的话,移动视频的四个角 ...

  4. string::rfind

    该函数返回目标字符串(key)在源字符串中最后一次匹配的位置 如果没有找到匹配的位置则返回string::npos,是一个无符号整形数字,可以打印看看 //// string::rfind #incl ...

  5. mongoDB 入门指南、示例

    一.准备工作 1. 下载mongoDB 下载地址:http://www.mongodb.org/downloads 选择合适你的版本 相关文档:http://www.mongodb.org/displ ...

  6. .Net自带的委托类型—Func,Action 和 Predicate

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递. 与其他的类不同,委托类具有一个签名,并且它只能对与其签名匹配的方法进行引用. 一.自定义委托类型 1.语法结构:访问修 ...

  7. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

  8. NYOJ-655 光棍的YY AC 分类: NYOJ 2013-12-29 19:24 224人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> char str[210]; int max[210][52]={0}; int sum(int n, ...

  9. MX记录

    是邮件交换记录,它指向一个邮件服务器,用于电子邮件系统发邮件时根据 收信人的地址后缀来定位邮件服务器.MX记录也叫做邮件路由记录,用户可以将该域名下的邮件服务器指向到自己的mail server上,然 ...

  10. html之colspan && rowspan讲解

    1.colspan && rowspan均在td标签中使用 2.每个单元格大小一致的前提 <table border="1" bordercolor=&quo ...