Announcement Announcement Module
No announcement yet.
Loading multiple context files Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loading multiple context files

    Here is the problem,

    1) I've an application with two layers: service and client (not web client) layers.
    2) Each layer has multiple modules.
    3) Each module has its own application context file
    4) We want to maintain only one bean factory per layer
    5) Client layer is dependent on service layer

    In order to load all modules of a layer in single bean factory (appContext-service.xml),

          <bean id="service.context" class="">
    In order to load this context,

      BeanFactoryLocator bfLocator = SingletonBeanFactoryLocator.getInstance("appContext-service.xml");
    		BeanFactoryReference bfReference = bfLocator.useBeanFactory("service.context");
    The above code shows how I am doing it for one layer. I want to do this for both layers and then make service layer parent of client layer.

    My questions,

    1) Is using "SingletonBeanFactoryLocator" ok? In the Spring's Javadoc it says try not to use SingletonBeanFactoryLocator - if not then what else can I use to load the "ClassPathXmlApplicationContext"?

    2) How do I make the parent-child relation between layers if both are loaded as SingletonBeanFactory?

    3) Is there any other better way of loading multiple application context files, without using SingletonFactory?


  • #2
    What type of client are you using? Is it running within an application server? When you say not a web client, do you mean there is not a web container, or just that there aren't JSPs and servlets?

    If the client is running in an app server, you can allow the container to manage the lifecycle of your application context through either the ServletContextLoader (if you have a web container) or a derivative thereof which can be associated with the container's lifecycle. Take a look at the code in that class to see how the SingletonBeanFactoryLocator is used.

    Spring's warning about not using SingletonBeanFactoryLocator is targeted at overuse, rather than no use. Whenever possible, you want to use dependency injection to inject relationships rather than looking up dependencies via this mechanism. If the lookup only resides in some glue code on container startup, it is considered an acceptable use of the factory locator.


    • #3
      It's not really client per se, but the middleware layer (business logic), which uses same generic service layer. Middleware may be run on the application server, but not as servlet.

      I've two main issues,

      1) How do I make the parent-child relation between layers if both are loaded as SingletonBeanFactory?

      2) Can I have nested "ClassPathXmlApplicationContext"? That is, if I've one xml, say a.xml, with ClassPathXmlApplicationContext (loading multiple context files) and then another, b.xml, with ClassPathXmlApplicationContext. Now is it possible to have c.xml that loads both a.xml and b.xml using ClassPathXmlApplicationContext? Then eventually I'll load c.xml using SingletonBeanFactory.



      • #4
        So who is the client, then? How is your business logic exposed to the outside world? A web service, an MDB, an RMI endpoint? I'm asking because if you have accessibility to a web container (whether or not you're serving up JSPs) and this is your client layer, you should use the ContextLoaderListener:

        The documentation there specifies how to do parent-child context loading so that your service layer is the parent context of your client layer. It uses the SingletonBeanFactoryLocator under the covers to obtain the parent context, then sets that context as the super-context of the web application context it is creating. The client itself can access the context using WebApplicationContextUtils, which does a lookup in the ServletContext for the newly-created Spring application context.

        If your client TRULY is not running in a web container but, say, is in an EJB container, you will need to write a context loader specific to that type of container... unless maybe there is one in Spring somewhere, but I haven't seen it. If you're not running in an app server at all, you will need to write some sort of centralized object which wraps the SingletonBeanFactoryLocator for its access and applies logic similar to what is done in the ContextLoader class.