简体   繁体   中英

how to instantiate a spring bean using a classloader?

I have a specific requirement that i should be dynamically (at runtime) able to load and unload bean classes in my Spring IoC container.

A bean needs to be

  1. created
  2. destroyed
  3. created again (from the same class, probable a new version of the same class)

the problem i am facing is that since the class in step 1 is loaded in system classloader, it remains there even when i destroy the bean. So in the step 3 i get an object of the old class, not the new class.

So the solution to my problem is to load the class in MyClassLoader instead of system classloader.

So my steps become

  1. create the specific classloader
  2. create the bean using specific class loader
  3. set the bean reference to null and remove bean from AppContext
  4. set the classloader reference to null

reload the bean using step 1 and 2

My Question is how to implement the above approach in Spring IoC container?

Thanks,

Regards,

Vimal

You can move some of your objects to a context which will be reloaded with a dedicated classloader. To make sure a proper classloader is used call context.setClassLoader(classLoader) before calling refresh or set thread classloader through Thread.currentThread().setContextClassLoader(classLoader) . Also keep in mind if classes you want to reload will be available to some common parent classloader, they won't be reloaded since parent classloader always takes precedence.

JRebel也许是一个解决方案?

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