Announcement Announcement Module
Collapse
No announcement yet.
How to ref several ApplicationContext objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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[].

    Comment


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

      Comment


      • #4
        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

        Comment


        • #5
          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 ?

          Comment


          • #6
            ... 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 ?

            Comment


            • #7
              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 ?

              Comment


              • #8
                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.

                Comment


                • #9
                  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 ?

                  Comment


                  • #10
                    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.

                    Comment

                    Working...
                    X