Difference between applicationContext.xml and spring-servlet.xml in Spring

Are applicationContext.xml and spring-servlet.xml related anyhow in spring framework? Will the properties files declared in applicationContext.xml be available to DispatcherServlet? On a related note, why do I need a *-servlet.xml at all ? Why is applicationContext.xml alone insufficient?

share | improve this question
 
 
up vote 101 down vote accepted

Spring lets you define multiple contexts in a parent-child hierarchy.

The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.

The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).

Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.

All Spring MVC controllers must go in the spring-servlet.xml context.

In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

share | improve this answer
 
4 
why would you have multiple spring servlets ? –  NimChimpsky Aug 14 '12 at 9:59
2 
mighty potent answer (because of succinctness) –  amphibient Mar 15 at 16:44
3 
@NimChimpsky it is sometimes useful to separate parts of your application that could otherwise conflict in the same context. As an example you may have ReST services and standard views, you may then have different view resolvers or security concerns for the services as to the views. –  Brett Ryan Apr 3 at 17:19
2 
People should see this answer before reading documentation and developing apps! In normal cases there is no need to have ContextLoaderListener and contextConfigLocation at all, just DispatcherServlet! –  ruruskyi Jun 28 at 13:11
2 
In many tutorials contextConfigLocation contains dispatcher-servlet.xml as well as DispatcherServlet. This causing beans to be initialized twice! –  ruruskyi Jun 28 at 13:24
show 2 more comments
No problem. We won't show you that ad again. Why didn't you like it?
Oops! I didn't mean to do this.

One more point I want to add. In spring-servlet.xml we include component scan for Controller package. In following example we include filter annotation for controller package.

<!-- Scans for annotated @Controllers in the classpath -->
<context:component-scan base-package="org.test.web">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

In applicationcontext.xml we add filter for remaining package excluding controller.

<context:component-scan base-package="org.test">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
share | improve this answer
 
1 
why ? Why not just scan the whole thing one time ? –  NimChimpsky Aug 14 '12 at 9:57
1 
@NimChimpsky You have to scan @Controller beans in servlet context (required by Spring MVC). –  Tuukka Mustonen Nov 21 '12 at 12:22
2 
Why not can the whole thing twice? Why include/exclude? –  Mike Rylander Jul 5 at 16:26

Scenario1. In client application(application is not web application.E.g may be swing app) private static ApplicationContext context = new ClassPathXmlApplicationContext("test-client.xml");

context.getBean(name);

No need of web.xml.ApplicationContext as container for getting bean service.No need for web server container. In test-client.xml there can be Simple bean with no remoting,bean with remoting. Conclusion:In Scenario1 applicationContex and DispatcherServlet they are not related.

Scenario2. In server application(application deployed in server e.g tomcat).Accessed service via remoting from client program(e.g swing app)

Define listener in web.xml

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

When server startup ContextLoaderListener instantiates beans defined in applicationcontext.xml. If there are suppose

<import resource="test1.xml" />
<import resource="test2.xml" />
<import resource="test3.xml" />
<import resource="test4.xml" /> in applicationcontext.xml

beans are instantiated from all four test1.xml,test2.xml,test3.xml,test4.xml. Conclusion:In Scenario2 applicationContex and DispatcherServlet they are not related.

Scenario3.In web application with spring MVC.In web.xml define. <

servlet>
        <servlet-name>springweb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>springweb</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

When tomcat starts bean defied in springweb-servlet.xml is instantiated. DispatcherServlet extends FrameworkServlet.In FrameworkServlet bean instantiation takes place for springweb .In our case springweb is FrameworkServlet.

Conclusion:In Scenario3 applicationContex and DispatcherServlet they are not related.

Scenario4.In web application with spring MVC.springweb-servlet.xml for servlet and applicationcontext.xml for accessing the business service within the server program Or for accessing DB service in another server program.In web.xml define.

org.springframework.web.context.ContextLoaderListener

<servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

When server startup ContextLoaderListener instantiates beans defined in applicationcontext.xml If there are suppose

<import resource="test1.xml" />
<import resource="test2.xml" />
<import resource="test3.xml" />
<import resource="test4.xml" />

beans are all instantiated from all four test1.xml,test2.xml,test3.xml,test4.xml. After the completion of bean instantiation defined in applicationcontext then bean defied in springweb-servlet.xml is instantiated. So instantiation order is root is application context ,then FrameworkServlet.

Now it makes clear why they are important in which scenario.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. web.xml:这是一个Java Web 项目的核心配置文件,主要用于配置Servlet、Filter、Listener等Web组件,并且定义了Servlet容器的一些基本配置,如编码、Session管理、错误页面等。其中,常用的配置包括: - 配置Servlet:用于处理HTTP请求的Java类。 - 配置Filter:用于对HTTP请求进行过滤和处理。 - 配置Listener:用于监听Web应用程序的生命周期事件。 2. springmvc-config.xml:这是一个Spring MVC框架的配置文件,主要用于配置Spring MVC的核心组件,如HandlerMapping、ViewResolver、Interceptor等。其中,常用的配置包括: - 配置HandlerMapping:用于映射请求到相应的控制器方法。 - 配置ViewResolver:用于将控制器方法返回的逻辑视图名映射到实际的视图模板。 - 配置Interceptor:用于拦截请求,在处理请求前或处理请求后进行一些操作,如权限控制、日志记录等。 3. spring-mybatis.xml:这是一个整合Spring和MyBatis框架的配置文件,主要用于配置数据库连接、事务管理、Mapper接口扫描等。其中,常用的配置包括: - 配置数据源:用于连接数据库,设置连接池等。 - 配置事务管理器:用于管理数据库事务,保证事务的一致性和可靠性。 - 配置Mapper接口扫描:用于自动扫描Mapper接口,并将其注册为Spring的Bean。 4. applicationcontext.xml:这是一个Spring框架的核心配置文件,主要用于配置Spring容器中的各种Bean,包括Service、DAO、Interceptor等。其中,常用的配置包括: - 配置Bean:用于定义Spring容器中的各种Bean。 - 配置AOP:用于实现面向切面编程,如事务管理、日志记录等。 - 配置属性文件:用于加载外部的属性文件,如数据库连接信息、邮件服务器信息等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值