Dynamic loading of modules in Java

后端 未结 2 844
陌清茗
陌清茗 2021-01-06 03:53

In Java, I can dynamically add stuff to classpath and load classes (\"dynamically\" meaning without restarting my application). Is there a known framework/library which deal

相关标签:
2条回答
  • 2021-01-06 04:31

    Perhaps the simplest approach is to load each plugin with it's own class loader. Then discard the class loader and create a new one to reload the plugin. You will want init() and destroy() methods in the plugin API to allow a chance for startup/shutdown type functionality.

    This also has the advantage of isolating the plugins from each other.

    A URLClassLoader is your starting point for this. The general idea is that you provide a XxxPlugin superclass that any plugin subclasses. Consider the example of Applet, which is essentially a GUI plugin (or Midlet, etc).

    0 讨论(0)
  • 2021-01-06 04:34

    You might consider running your spring application in an OSGI framework.

    I believe the DMServer is a module-based Java application server that is designed to run enterprise Java applications and Spring-powered applications, based on OSGI

    You can find more details in this Hello, OSGi, Part 2: Introduction to Spring Dynamic Modules article, in particular how to use Spring DM to dynamically install, update, and uninstall modules in a running system.


    Note: when you speak about "plugins can be upgraded or installed without restarting application", OSGI is the first candidate framework that comes to mind.

    It is all about modularization of applications into smaller bundles.
    Each bundle is a tightly-coupled, dynamically loadable collection of classes, jars, and configuration files that explicitly declare their external dependencies (if any).

    0 讨论(0)
提交回复
热议问题