gotch4
gotch4

Reputation: 13299

Spring configuration questions

I'm just entering Spring (on my own, so I've nobody to ask stupid questions).

I've a couple of questions about the configuration of Spring (I'm using 3.0). Reading the manual, it says that the "main" configuration is the one of the IoC container that instantiates all your beans. That way when you instantiate an ApplicationContext, you'll need to pass it the xml's with the bits of configuration for all your beans.

Anyway when you use the web MVC framework, you instantiate the DispatcherServlet, then you create an xml file with the name that includes the servlet config and you put your stuff there. Question one: is that all? I mean that xml file stands for the same files I could pass to the applicationcontext constructor or is that a different thing? Shall I create other files if I want to configure the whole applicationContext? How?

Going further I see that the dispatcher servlet may want more config. Like if you want to configure Views you have to write some config if you want to resolve a View bean and similar situations. Now I see many tutorials talking about a views.xml file... This is not even mentioned in the manual (cntrl+f not found). I wonder if that is a file that programmers create then include in the DispatcherServlet or is a default that some component reads... I'm confused about it.

I'd like to know more what's the relationship of a whole ApplicationContext configuration and the dispatcherservlet. More if it is the case that there are other xml files, I'd like to know where to find resources about that, because the docs are very confusing.

Thanks

Upvotes: 2

Views: 257

Answers (2)

Mauli
Mauli

Reputation: 17203

you can also split your applicationContext or servlet.xml by just defining multiple files in your web.xml

<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/spring/spring-beans1.xml
    /WEB-INF/spring/spring-beans2.xml
  </param-value>
</init-param>

have a look at this question Use a ContextLoaderListener in accordance with DispatchServlet

One thing I needed to understand when I started using spring was, that the default scope of the application context was singleton and that of the the DispatcherServlet request. That means although bot the ApplicationContext and the DispatcherServlet are startet when your application starts, if you get a bean from the application context it is always the same object as long as the application is running, while if you get a bean from the servlet.xml it is always newly instantiated for each http request.

see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes

The applicationContext is always automagically available within a servlet.xml

You can also get the applicationContext yourself using http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/support/WebApplicationContextUtils.html

Upvotes: 3

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299148

Anyway when you use the web MVC framework, you instantiate the DispatcherServlet, then you create an xml file with the name that includes the servlet config and you put your stuff there. Question one: is that all? I mean that xml file stands for the same files I could pass to the applicationcontext constructor or is that a different thing? Shall I create other files if I want to configure the whole applicationContext? How?

While in theory, you could define all beans in one monolithic web application context, it is usually considered good practice to split contexts by functionality.

e.g.

  • one context would define spring mvc web controllers
  • one would define the service layer
  • one would define the persistence / dao layer

And you would import these contexts into the main web context using <import ...> declarations.

Here's the relevant section in the spring reference:

Upvotes: 3

Related Questions