1

I came across below statement from Spring MVC documentation:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

Please help me in understanding this statement.

As global scope is used for portlet based applications, then why a developer wants to configure like this in normal Spring MVC applications?

tereško
  • 58,060
  • 25
  • 98
  • 150
Chaitanya
  • 15,403
  • 35
  • 96
  • 137

3 Answers3

2

I don't think the term "global scope" here means the global scope bean like singleton, prototype, request, session and global. I believe the global scope here means the scope of the bean context.

In Spring MVC, there are 2 scopes of bean can be defined. The first one is at servlet context level which I believe is what it meant by "global scope" in the above statement. The second one is at servlet level where the bean defined at this level will take priority when resolved by other bean in servlet level.

The beans in servlet level will be able to resolve beans in servlet context (global) level but not the other way round.

Wins
  • 3,420
  • 4
  • 36
  • 70
1

Spring MVC provides options to configure multiple Context based on number of DispacthServlet Configuration .
For example
Consider you have two modules with in your application and url pattern starts as module1/* and module2/* . And you want to keep two different context ( always declarations in *context.xml is private to the context ). So you would be creating two dispatchServlet and provide two different servletname and url pattern. Now you have two Spring context which has specific declartions and which is not visible to other. But still you may wanted to declare some application wide beans such as persistentManager / Logger instance and so. for those case you can keep seperate config as root-context.xml and keep the generic declartions in that root. Which is considered here as "GLOBAL SESSION"
Now this Global Session beans scope can be different scope based on configuration. And the bean configured in Global Session ( root-context) can be overriden by specific-servlet for customization.
I like the answer provided here also Understanding contexts in Spring MVC

Community
  • 1
  • 1
Mani
  • 3,274
  • 2
  • 17
  • 27
1

A typical Spring MVC application will have two contexts: the root context and the servlet context.

The root context is loaded by the ContextLoaderListener. The ContextLoaderListener loads the ApplicationContext and sets it as an attribute in the ServletContext. This makes it available to other Servlet, Filter, and XxxListener instances.

The servlet context is another ApplicationContext loaded by the DispatcherServlet when its init() method is called. Since the init() method of a Servlet is called after the contextInitialized() method of a ServletContextListener is called, the DispatcherServlet has access to the root context set as an attribute in the ContextLoaderListener and can therefore use the beans declared there.

[...] creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

A bean definition is, for example,

<bean id="someBean" class="com.company.SomeBean">
    <property name="someProp" value="some value" />
</bean> 

Spring creates a BeanDefinition object for this and all other <bean> declarations (or @Bean annotation methods). When the ApplicationContext is refreshed, Spring generates actual instances from these bean definitions.

What the above quote from the documentation is saying is that if you have a <bean> declaration with the same name or id in both the root context and the servlet context, the servlet context one will overwrite the root context one.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks a lot for clarifying, can you please tell me what it means: "When the ApplicationContext is refreshed"? – Chaitanya Dec 26 '13 at 12:36
  • @user Read the [javadoc](http://docs.spring.io/spring/docs/3.2.6.RELEASE/javadoc-api/org/springframework/context/ConfigurableApplicationContext.html#refresh()). Refreshing it just resets the context. – Sotirios Delimanolis Dec 26 '13 at 15:15