Initially I declared all my beans in dispatcher-servlet
and my application worked. Do I really need to have an applicationConte开发者_如何学编程xt.xml
file?
You don't necessarily need it, but it is a preferred way to separate application layers:
- in
dispatcher-servlet.xml
place only web-related stuff - controllers, view resolvers, converters, etc. - in
applicationContext.xml
put all services and daos, and other general configurations
Note that you'll have to declare a listener so that spring can load applicationContext.xml
:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Then the context from applicationContext.xml
will be the parent context, and the one in dispatcher-servlet.xml
will be the child context. The child sees the beans in the parent, but the parent does not see those in the child.
Also note that applicationContext.xml
is a default name. You can change the name or the path where it is seeked via the contextConfigLocation
<context-param>
You don't need an applicationContext.xml
file per-say. But you do need a xml file, even if it is only to tell Spring to auto-load all the beans in your application.
精彩评论