Java程序热加载组件Spring-Loaded
2014-03-09 00:00
134 查看
摘要: 项目地址: https://github.com/spring-projects/spring-loaded jrebel的免费版
Spring Loaded is usable on any bytecode that may run on a JVM, and is actually the reloading system used in Grails 2.
1.2.0 snapshot springloaded-1.2.0.BUILD-SNAPSHOT.jar
1.1.5 release springloaded-1.1.5.RELEASE.jar
The download is the agent jar and needs no further unpacking before use.
The verifier is being turned off because some of the bytecode rewriting stretches the meaning of some of the bytecodes - in ways the JVM doesn't mind but the verifier doesn't like. Once up and running what effectively happens is that any classes loaded from jar files (dependencies) are not treated as reloadable, whilst anything loaded from .class files on disk is made reloadable. Once loaded the .class file will be watched (once a second) and should a new version appear SpringLoaded will pick it up. Any live instances of that class will immediately see the new form of the object, the instances do not need to be discarded and recreated.
No doubt that raises a lot of questions and hopefully a proper FAQ will appear here shortly! But in the meantime, here are some basic Qs and As:
Q. Does it reload anything that might change in a class file? A. No, you can't change the hierarchy of a type. Also there are certain constructor patterns of usage it can't actually handle right now.
Q. With objects changing shape, what happens with respect to reflection? A. Reflection results change over time as the objects are reloaded. For example, modifying a class with a new method and calling getDeclaredMethods() after reloading has occurred will mean you see the new method in the results. But this does mean if you have existing caches in your system that stash reflective information assuming it never changes, those will need to be cleared after a reload.
Q. How do I know when a reload has occurred so I can clear my state? A. You can write a plugin that is called when reloads occur and you can then take the appropriate action. Create an implementation of
Q. What's the state of the codebase? A. The technology is successfully being used by Grails for reloading. It does need some performance work and a few smacks with a refactoring hammer. It needs upgrading here and there to tolerate the invokedynamic instruction and associated new constant pool entries that arrived in Java 7.
What is Spring Loaded?
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.
Installation
Version 1.2.0 includes preliminary Java8 support1.2.0 snapshot springloaded-1.2.0.BUILD-SNAPSHOT.jar
1.1.5 release springloaded-1.1.5.RELEASE.jar
The download is the agent jar and needs no further unpacking before use.
Running with reloading
java -javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify SomeJavaClass
The verifier is being turned off because some of the bytecode rewriting stretches the meaning of some of the bytecodes - in ways the JVM doesn't mind but the verifier doesn't like. Once up and running what effectively happens is that any classes loaded from jar files (dependencies) are not treated as reloadable, whilst anything loaded from .class files on disk is made reloadable. Once loaded the .class file will be watched (once a second) and should a new version appear SpringLoaded will pick it up. Any live instances of that class will immediately see the new form of the object, the instances do not need to be discarded and recreated.
No doubt that raises a lot of questions and hopefully a proper FAQ will appear here shortly! But in the meantime, here are some basic Qs and As:
Q. Does it reload anything that might change in a class file? A. No, you can't change the hierarchy of a type. Also there are certain constructor patterns of usage it can't actually handle right now.
Q. With objects changing shape, what happens with respect to reflection? A. Reflection results change over time as the objects are reloaded. For example, modifying a class with a new method and calling getDeclaredMethods() after reloading has occurred will mean you see the new method in the results. But this does mean if you have existing caches in your system that stash reflective information assuming it never changes, those will need to be cleared after a reload.
Q. How do I know when a reload has occurred so I can clear my state? A. You can write a plugin that is called when reloads occur and you can then take the appropriate action. Create an implementation of
ReloadEventProcessorPluginand then register it via
SpringLoadedPreProcessor.registerGlobalPlugin(plugin). (There are other ways to register plugins, which will hopefully get some documentation!)
Q. What's the state of the codebase? A. The technology is successfully being used by Grails for reloading. It does need some performance work and a few smacks with a refactoring hammer. It needs upgrading here and there to tolerate the invokedynamic instruction and associated new constant pool entries that arrived in Java 7.
Working with the code
git clone https://github.com/SpringSource/spring-loaded[/code]
Once cloned there will be some projects suitable for import into eclipse. The main project and some test projects. One of the test projects is an AspectJ project (containing both Java and AspectJ code), and one is a Groovy project. To compile these test projects in Eclipse you will need the relevant eclipse plugins:
AJDT: update site:http://download.eclipse.org/tools/ajdt/42/dev/updateGroovy-Eclipse: update site:http://dist.springsource.org/snapshot/GRECLIPSE/e4.2/
After importing them you can run the tests. There are two kinds of tests, hand crafted and generated. Running all the tests including the generated ones can take a while. To run just the hand crafted ones supply this to the JVM when launching the tests:-Dspringloaded.tests.generatedTests=false
NOTE: When running the tests you need to pass -noverify to the JVM also.
Two launch configurations are already included if you are importing these projects into eclipse, which run with or without the generated tests.
A gradle build script is included, run './gradlew build' to rebuild the agent - it will be created as something like: springloaded/build/libs/springloaded-1.1.5.BUILD-SNAPSHOT.jar
相关文章推荐
- java热加载之springloaded
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 在没有eclipse和Java环境下的程序代码练手--Spring事务管理
- 给Java的整个程序加上全局快捷键, 而不受限于是哪个组件得到焦点.
- Java Web系统初始化时从数据库中加载数据到文本(ibatis,spring)
- Java基础---Java---基础加强---类加载器、委托机制、AOP、 动态代理技术、让动态生成的类成为目标类的代理、实现Spring可配置的AOP框架
- 使用JavaService让java程序加载为winNT系统服务
- 由浅入深写java分布式(4)基于注解 dubbo 一个app同时存在consumer和provider自启动失败的问题,以dubbo和spring注解加载顺序的问题
- spring启动加载程序的几种方法介绍
- 命令行执行Java程序加载指定目录中的Jar文件
- cmd命令行下运行Java程序出现“找不到或无法加载主类”的错误
- java swing组件实现gui 日历程序
- 为Servlet或普通Java程序注入Spring托管的Bean、数据源
- JavaAgent 应用(spring-loaded 热部署)
- Myeclipse java程序中运行图片无法加载并且乱码