Java Spring-loaded热加载(Version:springloaded-1.2.8.RELEASE)

  • springloaded各个版本
  • Spring Loaded is usable on any bytecode that may run on a JVM, and is actually the reloading system used in Grails 2
    springloaded可以用于任何运行在jvm的字节码上的热更新,事实上,这是Grails2上的重载系统
  • 其实这东西还是基于agent的(org.springsource.loaded.agent.SpringLoadedAgent):
/*
 * Copyright 2010-2014 VMware and contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springsource.loaded.agent;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;

/**
 * Basic agent implementation. This agent is declared in the META-INF/MANIFEST.MF file - that is how it is 'plugged in'
 * to the JVM when '-javaagent:springloaded.jar' is used.
 * 
 * @author Andy Clement
 * @since 0.5.0
 */
public class SpringLoadedAgent {

    private static ClassFileTransformer transformer = new ClassPreProcessorAgentAdapter();

    private static Instrumentation instrumentation;

    public static void premain(String options, Instrumentation inst) {
        // Handle duplicate agents
        if (instrumentation != null) {
            return;
        }
        instrumentation = inst;
        instrumentation.addTransformer(transformer);
    }

    public static void agentmain(String options, Instrumentation inst) {
        if (instrumentation != null) {
            return;
        }
        instrumentation = inst;
        instrumentation.addTransformer(transformer);
    }

    /**
     * @return the Instrumentation instance
     */
    public static Instrumentation getInstrumentation() {
        if (instrumentation == null) {
            throw new UnsupportedOperationException("Java 5 was not started with preMain -javaagent for SpringLoaded");
        }
        return instrumentation;
    }

}

  • 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.
    JVM运行时重新加载类文件更改。它在加载时转换类,使它们适合以后重新加载。与“热代码替换”不同,“热代码替换”仅在JVM运行时间允许简单更改(例如,更改方法体),Spring Loaded允许您添加/修改/删除方法/字段/构造函数。还可以修改类型/方法/字段/构造函数的注释(例如@RequestMapping),并且可以在枚举类型中添加/删除/更改值
  • 使用:java -javaagent:/springloaded-{VERSION}.jar -noverify SomeJavaClass只要修改class文件,立即生效,SomeJavaClass修改无效
  • Once loaded the .class file will be watched (once a second) and should a new version appear SpringLoaded will pick it up,一秒一次
  • tomcat 使用spring-loaded
    的话,可以在catalina.sh/catalina.bat中JAVA_OPTS="$JAVA_OPTS -javaagent:/springloaded-{VERSION}.jar -noverify"
  • 在IDEA中使用:
    • 先确保你的idea会自动编译

      • CMD + SHIFT + A –> 查找 make project automatically –> 选中
      • CMD + SHIFT + A –> 查找 Registry –> 找到并勾选compiler.automake.allow.when.app.running
    • Run->Edit Configurations->VM options添加JVM参数-javaagent:/springloaded-{VERSION}.jar -noverify

    • 用idea打开maven的web项目这么干是无效的,编译后class的存放位置和web项目运行目录位置不一样,就算开启自动编译或者手动编译都会到target/classes/目录下,而项目运行目录在target/项目名/目录下

    • 目运行目录在`target/项目名/`目录下
    • 临时方案:

      • Maven 编译Web项目到新目录
      • mvn install 可以重新编译到target/web/WEB-INF/classes目录下,同时设置target/web作为运行目录,mvn install 后spring-loaded会热加载新文件
        IDEA设置`target/web`作为运行目录

你可能感兴趣的:(Java Spring-loaded热加载(Version:springloaded-1.2.8.RELEASE))