Announcement Announcement Module
No announcement yet.
How to use spring IoC for beans created by with java.util.ServiceLoader Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use spring IoC for beans created by with java.util.ServiceLoader

    I am using java.util.ServiceLoader to dynamically load plugin implementations of some service provider interfaces in my app. I now need to wire the plugin implementation classes using spring. This means that I somehow need to use the following package in spring:

    Sadly, I cannot grok how to use this API from the available docs. Can any one please give me a hint. Thanks.

    --Farrukh Najmi

  • #2
    Is a FactoryBean, read about FactoryBeans in reference docs. Simply declare a bean of class ServiceFactoryBean, set the serviceType property and inyect it:

    <bean id="prettyService" class="org.springframework.beans.factory.serviceloade.ServiceFactoryBean" >
    <property name="serviceType" value="" />
    <bean id="wantToBePretty" class="...">
    <property name="prettyService" ref="prettyService" />


    • #3
      Thanks Jose Luis, that was very helpful! I have one followup question.

      My server loads the service class for its plugin interface from a dynamically loaded plugin jar from a certain location.
      If the plugin jar uses spring to configure the service class how does the server use the org.springframework.beans.factory.serviceloader.Se rviceFactoryBean such that the ServiceFactoryBean instantiates the service bean using spring? Are there any tricks that will allow me to do this?

      Thanks again.


      • #4
        I am still looking for the best practices on how to use spring IOC to inject dependencies into a bean that implements a service interface and is created by the ServiceLoader class. Does any one have any experience to share?

        Here is a summary of my design:
        • My server is a webapp running in a servlet container like Glassfish 3 or Tomcat 7
        • Various plugin interfaces are defined by my server
        • A jar may implement a server extension that contains implementations for one or more plugin interfaces
        • Extension jars may be placed in a ".../plugins" directory for the server to load dynamically
        • Server uses spring IoC / dependency injection to wire nearly all of its beans
        • Server uses a CustomClassLoader that extends URLClassLoader to load extenion jars from plugins directory
        • An extension of spring's org.springframework.beans.factory.serviceloader.Se rviceListFactoryBean uses the CustomClassLoader to create service beans (implementations of plugin interfaces)
        • Spring XML application context files are defined for woring the plugins for each plugin jar. These files use a naming convention like *-dynamic-context which allows server to create an application context for each plugin jar as a child of the main application context for the server

        The problem is that the ServiceListFactoryBean uses a java.util.ServiceLoader to create an instance of the service bean by using its argumentless constructor. It does not use a extensions application context to create the bean.

        Is there any clean solution to this problem?

        TIA for sharing your experience.