Is there a way in Spring to discover a "plugin"-JAR from the classpath, and load its a开发者_开发问答pplicationContext.xml dynamicaly?
I have achieved a plugin-like system with Spring by following this approach:
Each plug-in must contain a spring-context file with a specific name and package prefix (for example, com.example.myApp.whatever containing plugin.xml, or applicationContext.xml if you prefer).
For the plug-in to be detected in the classpath, the host application should dynamically import all the context files contributed by any jar following the previos scheme. This is achieved with a wildcard-based import in spring config:
<import resource="classpath*:/com/example/myApp/**/plugin.xml" />
Provided that each plug-in defines beans of a known interface (e.g., MyInterface). The host application can define a property of type List <MyInterface> and define the bean as autowire="byType" in order to retrieve all the beans of the MyInterfaceType in a list.
精彩评论