在java spring中,是否可以动态将一个新的class加载到beanFactory中?是否可以将class类以字节流的方式存到redis中,再由类加载器重新加载到java运行实例中?
目前项目中有一个需求,需要可以动态地加载类到spring的beanFactory中或者说applicationContext里面,这个类是可以由开发人员动态上传到生产环境中,而无需重新启动生产环境。请问是否可以做到呢?还有一个问题,是否可以将class文件以字节的方式暂存在redis中间件上,需要用这个class时,动态地加载它?
After JDK1.5, there is a ready-made API java.lang.instrument.Instrumentation to implement this function.
The above interface can redefine the existing one
class
.Use as below.
Method to get
Instrumentation
object:agent.jar
打包时需要指定Agent-Class
,Can-Redefine-Classes
Enable class rewriting function.Agent.java
RealizationHere I injected the
It’s because the running environment ofJMX
将Instrumentation
对象注入过去的,当时为什么要这样来做呢。是因为
vm.loadAgent();
运行的环境是一个全新的,我无法使用静态方法来设置属性,虽然类相同但是加载该个类的ClassLoader
却不一致。后面我尝试出可以使用JMX
object throughJMX
. Why did I do it this way?vm.loadAgent();
is a brand new one, and I cannot use static methods to set properties. Although the classes are the same, theClassLoader is inconsistent.
Later I tried to call it usingJMX
.Instrumentation
Okay, as long as you get theTry to get the AutowireCapableBeanFactory through ApplicationContext, and then call the createBean() or autowire() method to inject it
Java has object input and output streams, so you can definitely store objects in bytes
None of what is said above is correct. I can tell you clearly that everything is possible.
mark to learn