Results 1 to 4 of 4

Thread: MVC, ApplicationContexts - best practice

  1. #1
    Join Date
    Mar 2007
    Posts
    561

    Default MVC, ApplicationContexts - best practice

    Hi,

    when you create a MVC project you have to define the Dispatcher-Servlet like this:

    Code:
      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    And the internal application-context like this:

    Code:
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
      </context-param>
    My question is:

    Why do we need different contexts here, one for the Dispatcher-Servlet and one for all the other beans?

    How can I merge them into one? Should I?
    I already tried to omit the contextConfigLocation in web.xml and use import in webmvc-config.xml instead, but this does not work.

    Background of my question:
    I have an applicationContext-aop.xml with common advices (tracing, logging etc) which should be used for the web-context and the internal context. I have worked around this problem by importing applicationContext-aop.xml in webmvc-config.xml, but this seems to be a bit strange because all my aop stuff is now loaded twice?

    Thank you

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    There is not really an easy answer to this... It basically depends.

    If you use Filters and Listeners which require Spring beans you need a ContextLoaderListener as those don't have access to the ApplicationContext loaded by the servlet (there can be multiple servlets each with its own ApplicationContext and a Filter/Listeners spans servlets). However you could load everything (including your web related things) with the ContextLoaderListener and nothing for the DispatcherServlet.

    If you don't use Filters/Listeners you could suffice with only a DispatcherServlet but in general you have something like an OpenSessionInViewFilter etc.

    Regarding your aop this isn't strange at all... Aspects only operate on the beans in the context as the Aspect resides in, I wouldn't want the aspect configuration in my root context influence all my other contexts. What you can do is load the advice in the root context and specify the pointcut in the context where you need it (note doesn't work with @Aspect obviously) this requires the aop:config and aop:aspect elements to be used.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2007
    Posts
    561

    Default

    What I do not understand: Why do I need multiple contexts? I have only one webapp and I do not want to reloads parts of it.

    "However you could load everything (including your web related things) with the ContextLoaderListener and nothing for the DispatcherServlet."

    How can this be done? Do I have to use an empty bean definition xml for the servlet?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Quote Originally Posted by spgmx
    What I do not understand: Why do I need multiple contexts? I have only one webapp and I do not want to reloads parts of it.
    It isn't a must but it is a seperation of concerns (kind of) als it isn't about 1 or multiple web apps but the amount of servlets you have. I once had an application which had multiple dispatcherservlets and also messagedispatcherservlets (webservices) now in that case it it really nice to have a root context and multiple seperate contexts for the different servlets (which don't influence each other).

    Quote Originally Posted by spgmx
    How can this be done? Do I have to use an empty bean definition xml for the servlet?
    No... Simply specify an empty value for the contextConfigLocation for the servlet.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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