3

In my web.xml I have removed contextConfigLocation and instead of pointing to application context I defined my beans in my dispatcher-servlet. Is that allowed or Spring looks for contextConfigLocation for sure?

<!--context-param>        //This part is commented. Is this allowed?
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener-->

By beans inside dispatcher servlet...

<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="oracle.jdbc.driver.OracleDriver"
        p:url="jdbc:oracle:thin:@localhost:1521:Xe"
        p:username="hibernate"
        p:password="hibernate"></bean>


Rahul Bussa
  • 258
  • 1
  • 3
  • 14

1 Answers1

4

Yes, but they will be visible in web context only. Look at this answer Spring can't see beans between servlet-context and contextConfigLocation beans

Quote from the Spring Framework API (at the moment of writing 3.2.2) for the WebApplicationContext:

Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.

Also here: Context hierarchies:

For example, if you are developing a Spring MVC web application you will typically have a root WebApplicationContext loaded via Spring's ContextLoaderListener and a child WebApplicationContext loaded via Spring's DispatcherServlet. This results in a parent-child context hierarchy where shared components and infrastructure configuration are declared in the root context and consumed in the child context by web-specific components.

And here: 17.2 The DispatcherServlet:

ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.

Not the last sentence:

you can define new scope-specific beans local to a given Servlet instance

Community
  • 1
  • 1
zacheusz
  • 8,750
  • 3
  • 36
  • 60