Announcement Announcement Module
No announcement yet.
Referring to an application context Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referring to an application context

    Hi all,
    My package structure is like this :
    src--> com -> A, src-->com ->B, src-->com ->C
    Each of A,B,C packages conatain XML bean definitions.
    In the "com" package I've "main" class in which I load all the xml files in one shot ("new ClasspathApplicationContext")
    After all xml s are loaded "context" is ready.

    I want to use this context in each of the modules (A, B, C) after it is ready, for "getBean" purposes.

    One way is to make this context static in the "main" class and let other modules access it by "MainClass.context".

    This is least preferred way. Is there any solution so that in each module I can refer to ready "application context"?


  • #2
    Not sure of your app needs, but the 'best practice' approach is to not make your application aware of the Spring app context. Since it seems this is a single JVM, you should not be using getBean, but dependency push, the com package loads the config files and the A, B, C package classes are DI.


    • #3
      Referring to an application context

      For this DI to take place, what should be the parameter setter (constructor) injection?
      I guess it should not be "main application context" else A,B.C package classes need to call "getBean".

      Should it be actual objects got by "getBean" in the main class? I mean "com" package "main" class issues "getBean" calls and pushes these objects to individual A,B,C package classes?

      Can you pls explain in detail so that I've clear pic?



      • #4
        When you define a bean in one of the package bean definition files you simply set a dependent bean from another package on each bean definition.

            <bean name="packAbean"  class=".......packA.PackABean">
                 <property name="packBbean" ref="packBbeanName"/>
        This results in packAbean being DI with the bean from packB. Of course, this assumes a very simple environment, that the bean definition files are combined or in a hierarchy, for the bean ref to work. No getBean calls in the code are required.