Announcement Announcement Module
Collapse
No announcement yet.
Root Application Context vs WebApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Root Application Context vs WebApplicationContext

    hi,

    I am working with spring and while doing it, got one query. Things which i didnt get is as follows, help me to remove the fog.
    1- Can i define service java files into Dispacher-servlet? if yes what will be the scope?
    2- I understood that rootApplicationContext(by ContextLoderListner and ContextConfigLocation) is loaded by webApplcationContext(by dispacher-servlet), and both are independent. RootApplicationContext can have multiple xml location, and all this specified xml in ContextConfigLocation make a single RootApplicationContext. Whatever i understood is Right here? if yes then what we need to define in WebApplicationContext and RootApplicationContext?


    Thanks

  • #2

    1- Can i define service java files into Dispacher-servlet? if yes what will be the scope?
    Everything in spring is singleton by default unless specified otherwise.


    2- I understood that rootApplicationContext(by ContextLoderListner and ContextConfigLocation) is loaded by webApplcationContext(by dispacher-servlet), and both are independent. RootApplicationContext can have multiple xml location, and all this specified xml in ContextConfigLocation make a single RootApplicationContext. Whatever i understood is Right here? if yes then what we need to define in WebApplicationContext and RootApplicationContext?
    Wrong...

    The ContextLoaderListener loads the xml files specified by the context parameter named 'contextConfigLocation' this ApplicationContext (which actually is a XmlWebApplicationContext by default) is registered as the root context. That root context is used by the ApplicationContext constructed by the DispatcherServlet as a parent ApplicationContext, by default the DispatcherServlet loads a xml file named [servletname]-servlet.xml....

    Normally you define your application beans in the root context (services, daos, datasource etc) and all web related beans (controllers, views etc) in the DispatcherServlet. So if you have multiple servlets you can reuse the single root context and beans in there.

    Comment


    • #3
      Thanks Marten!!
      Some how i was not able to mention my problem correctly but i was meaning the similar but still u correct it.
      From your reply i understood that "A Single WebApplicationContext created by ContextLoaderListener/ContextLoaderServlet, this created Context is inherited by DispacherServlet(which is again another Context), so we can override beans in context created by DispacherServlet"

      Again say you have define 3 (Context-1.xml, Context-2.xml ...) xmls in ContextConfigLocation parameter, And one xml having BeanPostProcessor(say PropertyPlaceholderConfigure in Context1.xml) so will it be apply to other xml (say Context-2.xml) ? Will it Apply to applicationContext created by DispacherServlet?(i guess ans No for this)

      Comment


      • #4
        All xml files specified are loaded in the same ApplicationContext, Bean(Factory)PostProcessors operate on all the beans in the ApplicationContext they are defined in. They will NOT operate on beans in parent and child configurations...

        Comment


        • #5
          Alright i got how its working.

          Every Dispacher-Servlets represent ChildContext, which can see the RootApplicationContext but not other Sibling Dispacher-Servlet. BPP or BFPP define in Root cant see Beans define in Child Context(that is Dispacher-Servlet)

          What is the scope/Visibility of BFPP/BPP define in a XML? will it gets broader if <import> tag used?

          Comment


          • #6
            the import tag will import the xml which will load that xml as part of the application context.

            Code:
            <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>context1.xml, context2.xml, context3.xml</param-value>
            </context-param>
            results in the same application context as
            Code:
            <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>root-context.xml</param-value>
            </context-param>
            
            -- root-context.xml
            <import resource="context1.xml" />
            <import resource="context2.xml" />
            <import resource="context3.xml" />
            If you would import one of those xml files (or the root-context.xml) in your dispatcher servlets xml file you basically end up with duplicate bean instances because they are treated as part of the context they are loaded in...

            Comment


            • #7
              Code:
              <context-param>
                <param-name>contextConfigLocation</param-name> 
                <param-value>context1.xml, context2.xml, context3.xml</param-value>
              </context-param>
              
              And Another is 
              
              Dispacher-Servlet.xml

              Context1.xml has defined PropertyPlaceholderConfigurer and all 4 xml(including Dispacher-Servlet.xml) having ${} in bean property, so PropertyPlaceholderConfigurer will replace in the xml in which its defined or in entire Root Application Context(that is Contex1,Context2,Context3 xml)?

              I asked above question bcoz you just mention how root application context and webApplicationContext loaded and its hierarchy.
              Last edited by rohan123; Aug 30th, 2011, 04:17 AM.

              Comment


              • #8

                Context1.xml has defined PropertyPlaceholderConfigurer and all 4 xml(including Dispacher-Servlet.xml) having ${} in bean property, so PropertyPlaceholderConfigurer will replace in the xml in which its defined or in entire Root Application Context(that is Contex1,Context2,Context3 xml)?
                As I stated

                Originally posted by mdeinum
                All xml files specified are loaded in the same ApplicationContext, Bean(Factory)PostProcessors operate on all the beans in the ApplicationContext they are defined in.
                Which answers your question... It doesn't matter if you have 1 xml file or 1000 xml files they all make 1 ApplicationContext (if they are loaded in the same ApplicationContext).

                I strongly suggest you read the reference guide which basically explains all of this...

                Comment


                • #9
                  Thanks Marten, clearing my doubts, i got it and i already read but Ref doc didnt clear my this things, then i post it here, Again Thanks a ton

                  Comment


                  • #10
                    Spring Doc is awesome, but you hepled me too Marten. Not everything is as clear as it seems. Thx a lot.

                    Comment

                    Working...
                    X