Results 1 to 10 of 10

Thread: How to ref several ApplicationContext objects

  1. #1
    Join Date
    Sep 2005
    Location
    Milan, Italy
    Posts
    23

    Default How to ref several ApplicationContext objects

    I have several libraries which are internally using spring and encapsulate their components exposing their just their ApplicationContext's...
    How can I instantiate my main application context with several .xml configurations and reference those other 'library' context's (there are 4,5 of them), as my main app beans are referencing those in the 'libraries' contexts ? I need something that permits several 'parent' context... is it possible ?

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    How are you loading your context? If you use ContextLoaderListener, you can specify several context files. Likewise if you use ClassPathXmlApplicationContext, the constructor can take a String[].

  3. #3
    Join Date
    Sep 2005
    Location
    Milan, Italy
    Posts
    23

    Default

    Yes but I have nedd of ClassPathXmlApplicationContext with MULTIPLE 'parent' contexts so my beans can reference them...

  4. #4

    Default

    Hello,

    The idea is not to have one applicationContext with several parents, but to configure your applicationContext in a way it contains all needed beans.

    All your libraries have one or several xml files describing their beans, you have to instanciate your applicationContext with all these xml files.

    (Parent child relation for applicationContext may be used to layer your system...)

    -Patrick

  5. #5
    Join Date
    Sep 2005
    Location
    Milan, Italy
    Posts
    23

    Default

    I think that what I am asking is something very simple... I don't want to couple my main module to other modules... I want to use them transparently, without having to know names of their .xml files...
    Is it possible ?
    If this is not possible can I then merge all dependent bean factories and pass that merged context as a parent ?

  6. #6

    Default

    ... as my main app beans are referencing those in the 'libraries' contexts ...
    I don't want to couple my main module to other modules...
    In some way you are coupled to other modules...

    How will the applicationContexts be created for your libraries ?

  7. #7
    Join Date
    Sep 2005
    Location
    Milan, Italy
    Posts
    23

    Default

    I have something like (pseudo-code):

    Library1 lib1 = new Library1();
    Library2 lib2 = new Library2();

    lib1.getContext();
    lib2.getContext();

    so I have 2 contexts which I want to use when doing:

    String[] configs = { "config1.xml", "config2.xml", "config3.xml" };
    appContext = new ClassPathXmlApplicationContext(configs, parent);

    in place of 'parent' I need merged lib1Context + lib2Context

    Is this possible ?

  8. #8
    Join Date
    Jul 2005
    Posts
    105

    Default

    Quote Originally Posted by sdmiski View Post
    I have something like (pseudo-code):

    Library1 lib1 = new Library1();
    Library2 lib2 = new Library2();

    lib1.getContext();
    lib2.getContext();

    so I have 2 contexts which I want to use when doing:

    String[] configs = { "config1.xml", "config2.xml", "config3.xml" };
    appContext = new ClassPathXmlApplicationContext(configs, parent);

    in place of 'parent' I need merged lib1Context + lib2Context

    Is this possible ?
    Yes, you can do this, but I'm not really sure why you would want to. I've found that I don't need more than one level of parent. A good example of when this might be needed is when I have a WebApplicationContext (with web-specific beans in them) who is the child of a global ClassPathApplicationContext (which defines the service layer).

    If you absolutely need to do this, though, you can't have the "multiple inheritance" scenario you're talking about. You have to pick one to be the grandparent and one to be the parent, and pray that the parent doesn't have any beans defined with the same name as the grandparent.

  9. #9
    Join Date
    Sep 2005
    Location
    Milan, Italy
    Posts
    23

    Default

    So you are telling me that my only solution is to make some weird parent-child relationships between my library factories even if those have no relationships whatsoever ?

  10. #10
    Join Date
    Jul 2005
    Posts
    105

    Default

    Are these third-party libraries you are talking about? If so, what kinds of beans are you trying to access from their application contexts? It's definitely an unusual situation.

    It sounds like what you are asking for Spring OSGi may solve. That would isolate the application contexts of each library you wanted to include, exposing only the beans that are public for you to use. You can then declare a reference to any public beans exposed in libraries.

    Spring OSGi, however, is still in its infancy. Definitely something to watch out for in the future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •