Spring Loaded is a JVM agent for reloading class file changes

            <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>
时间: 2024-10-25 13:58:39

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

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

在微服务天堂中Ratpack和Spring Boot是天造地设的一对.它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率.效率以及轻量级部署.他们在服务程序的开发中带来了各自的好处.Ratpack通过一个高吞吐量.非阻塞式的web层提供了一个反应式编程模型,而且对应用程序结构的定义和HTTP请求过程提供了一个便利的处理程序链:Spring Boot集成了整个Spring生态系统,为应用程序提供了一种简单的方式来配置和启用组件.Ratpack和Spring Boot是构建原生支持计

使用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 redefin

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

使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中文地址:使用Ratpack与Spring Boot构建高性能JVM微服务. 在微服务天堂中Ratpack和Spring Boot是天造地设的一对.它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率.效率以及轻量级部署.他

Spring Boot 官方文档入门及使用

个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念,请先移步上一篇文章 Spring Boot 学习.本篇原本是为了深入了解下Spring Boot而出现的. 另外,Spring Boot 仍然是基于Spring的,建议在赶完工之后深入学习下Spring,有兴趣可以看看我的 Spring 4 官方文档学习(十一)Web MVC 框架 .欢迎探讨,笑~

玩转spring boot——properties配置

前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连接,若有一处配错或遗漏,就会带来不可挽回的损失.正因为这样,spring boot给出了非常理想的解决方案——application.properties.见application-properties的官方文档:http://docs.spring.io/spring-boot/docs/curr

Implementing a java agent to instrument code (copy from http://chimpler.wordpress.com/2013/11/05/implementing-a-java-agent-to-instrument-code/)

With a system running 24/7, you have to make sure that it performs well at any time of the day. Several commercial solutions exist to monitor the performance of systems: NewRelic, GraphDat and many others. They allow to see for instance if the api ca

Spring boot 内存优化

转自:https://dzone.com/articles/spring-boot-memory-performance It has sometimes been suggested that Spring and Spring Boot are “heavyweight”, perhaps just because they allow apps to punch above their weight, providing a lot of features for not very muc

JDWP Agent

Implementation Description Revision History Disclaimer 1. About this Document 1.1 Purpose 1.2 Intended Audience 1.3 Using This Document 1.4 Conventions and Symbols 2. Overview 2.1 About JPDA 2.2 The JDWP Agent 2.2.1 Key Features 2.3 JDWP Tests 2.3.1

Spring Boot application.properties

1 # =================================================================== 2 # COMMON SPRING BOOT PROPERTIES 3 # 4 # This sample file is provided as a guideline. Do NOT copy it in its 5 # entirety to your own application. ^^^ 6 # =======================