简体   繁体   中英

Change class on runtime using javassist + spring + tomcat

I want to add 1 annotation to a class with javassist library. The class is loaded once before and I know we can't reload such a class in regular cases. javassist HotSwapper can do that but i don't know which port is listening. I get a connection refused exception when calling this code:

HotSwapper hs = new HotSwapper(8000);  // 8000 is a port number.

What is the port? Java debug port? Can I use it when the code is in running mode? I don't know how i can reload the class in runtime. Is it possible when we are using spring and tomcat? How can I run my code before loading any of other jar files in class path?

Thank You

What is the port? Java debug port?

Yes, you need something like this in your VM parameters:

-agentlib:jdwp=transport=dt_socket,address=localhost:8000,server=y,suspend=y

Can I use it when the code is in running mode?

Yes. Agent does not impact performance signicantly (as I know, I'm not sure), but other things (which connected to jdwp port) of course may impact.

I don't know how i can reload the class in runtime. Is it possible when we are using spring and tomcat?

You can try to call reload method of HotSwapper . Check this example from javadoc ( link to javadoc ):

 CtClass clazz = ...
 byte[] classFile = clazz.toBytecode();
 HotSwapper hs = new HostSwapper(8000);  // 8000 is a port number.
 hs.reload("Test", classFile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM