Spring startup jar

My project uses a simple plugin mechanism based on several application contexts defined in plugin banks. However, for this to work, I must include all the plugin cubes in the classpath. It would be nice if Spring could automatically load jars and containing components on its own, which, for example, are placed in the plugins subdirectory of my project.

Is there any solution for this?


I went a little further and tried to solve this with the Jar Class Loader .

Since I launch the Spring application context manually, I can do the following:

GenericApplicationContext ctx = new GenericApplicationContext(); // Load context definitions from plugin jars JarClassLoader jcl = new JarClassLoader(); jcl.add("plugins/"); XmlBeanDefinitionReader classPathBeansReader = new XmlBeanDefinitionReader(ctx); classPathBeansReader.setBeanClassLoader(jcl); classPathBeansReader.setResourceLoader(new PathMatchingResourcePatternResolver(jcl)); classPathBeansReader.loadBeanDefinitions("classpath*:META-INF/my-plugins-*.xml"); 

However, this does not work. From the Spring magazine, I see that it does not read the XML definition in the plugin bank. If I replaced the bottom block with

 XmlBeanDefinitionReader classPathBeansReader = new XmlBeanDefinitionReader(ctx); classPathBeansReader.setBeanClassLoader(jcl); classPathBeansReader.loadBeanDefinitions(new ClassPathResource("META-INF/my-plugins-somemodule.xml",jcl)); 

it finds and loads the XML definition file and beans from the jar. However, this way I am setting up the XML resource name for one plugin, which I don’t wan't. How can I map a template to JCL?

+8
java spring plugins classloader
source share
2 answers

You may like to use OSGi as a plugin loading mechanism.

The Eclipse Virgo project provides an OSGi runtime suitable for your project since it has Spring built-in. Virgo offers Tomcat and Jetty servers and a standalone kernel that can be used independently or to create other types of servers. See Virgo Website for features and benefits .

OSGi has a completely different design point than you can use in Java. This gives you controlled isolation between plugins known as bundles, as opposed to a linear class. Bundles are linked together in a dependency graph and support version control and dynamic life-cycle operations.

The preferred tool for a package to take advantage of other packages is the OSGi registry. The Spring DM project allows you to publish normal Spring beans in the service registry and look for it from the service registry. Spring DM is also built into Virgo. Spring DM was donated by Eclipse as a Twins project.

To use Virgo, you would add the Spring DM configuration for each of your plugins to the META-INF / spring directory. This configuration, which is a regular Spring XML configuration file, can refer to beans in your other Spring files and publish these beans to the service registry, or it can provide beans for services viewed by the service registry, which can then be referenced and pasted into beans in your other Spring files.

Then you placed your plugins in Virgo using any of the supported mechanisms. You can simply drop them in the dependency order into the pickup catalog. Or you can use the web admin console or the console console for deployment.

Alternatively, and this seems to fit your requirement quite well, you can place plugins that provide packages for other plugins in the Virgo repository, drop them into the / usr repository, and then deploy plugins that depend (transitively) on repository plugins through the pickup folder or web admin console. Virgo automatically deploys dependencies from the repository as dependent plugins are deployed.

You can also group plugins together in an archive known as PAR, or save them in the Virgo repository, and then refer to them in an XML file known as a plan. You will then deploy PAR or plan as described above. You can even put some of the dependencies in the Virgo repository and reduce PAR or plan to contain only dependent plugins.

If you want more information about Virgo, just ask the Virgo community forum .

+4
source share

JCL doesn't seem to override ClassLoader # findResource (String)

JarClassLoader.java AbstractClassLoader.java

PathMatchingResourcePatternResolver JavaDocs Property :

Internally, this is done by calling ClassLoader.getResources ()

JavaDocs for ClassLoader #getResources (String) overloads the documentation for ClassLoader # findResource (String), which states:

Finds a resource with the given name. Loader class implementations should override this method to indicate where to look for resources.

So, although my answer is based on reading a few bits of documents, I would suggest that the JCL does not support this because it does not override the documented methods.

You can verify this by subclassing the JarClassLoader and implementing findResource (String) to test my hypothesis.

Of course, I could be wrong.

+1
source share

All Articles