Difference Between Applicationcontext.Xml and Spring-Servlet.Xml in Spring Framework

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

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.

spring mvc applicationcontext.xml and spring servlet.xml difference

What you refer as applicationContext.xml is the root application context (you put beans there when you need application-wide access to them) and what you refer as the [servlet]-context.xml is a specific Spring Bean configuration xml for Spring MVC's DispatcherServlet.

The difference between web.xml, beans.xml, applicationContext.xml, etc

web.xml is a file that should reside in all J2EE web application. Its specification is defined by J2EE spec. Here you configure a general behaviour of your app. For example servlets, filters, security policy etc.

dispatcher-servlet is a special servlet in Spring MVC framework. You must define its mapping in your web.xml to enable Spring in your web app.

beans.xml is a file that is use for configure some CDI settings. For example bean-discovery-mode="annotated" means that only classes annoteded with CDI scope annotation will be consider as CDI managed beans.

applicationContext.xml here you are actually right. It is the common name of the main Spring configuration file. You can set many things here like for example create and wire some Spring beans.

glassfish-web.xml is generally an extension to web.xml for GlassFish server. It is not always needed. You need it if want to configure some settings specially for GlassFish server. For example if you configure the security part in your web.xml you have to map user roles from web.xml to GlassFish realm roles.

Hope it helps.

When will we use applicationContext.xml in Spring?

Why do we need applicationContext.xml in Spring?

In the early days of Spring framework, Application context i.e the various weave and settings necessary to bootstrap, coordinate and control all objects, where done using XML file. Although one can break various settings and dependency injection into several context files, this process has been made easier In Spring 2.5 and later by annotation-driven settings.

What is the difference between applicationContext.xml and spring-servlet.xml?

In a MVC based project, again if you're not using annotation-driven weaving mechanism for your project, all your endpoint servlets can be setup in the spring-servlet.xml. Note that the name of the file is always self chosen.

How can we compare applicationContext.xml in Spring with Struts.xml in Struts for easy understanding?

They are both similar in terms of what they're trying to achieve. i.e a central location for the application bootstrap settings. Similarly, all settings can be tiered into different files to make it modular.

Web.xml(tomcat) vs ApplicationContext.xml(spring) which loads first?

Spring MVC is a framework built on top of the Servlet API. As such, it requires a Servlet container.

Your Servlet container finds the web.xml or uses any other deployment strategy (depends on container), finds the Servlet implementations to instantiate (or through @WebServlet) and instantiates them.

One of these Servlet implementations is Spring's DispatcherServlet which, as part of its initialization, generates an ApplicationContext which it then uses to configure its controllers and dispatch requests.

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

Web Application context extended Application Context which is designed to work with the standard javax.servlet.ServletContext so it's able to communicate with the container.

public interface WebApplicationContext extends ApplicationContext {
ServletContext getServletContext();
}

Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface

package org.springframework.web.context;
public interface ServletContextAware extends Aware {
void setServletContext(ServletContext servletContext);
}

There are many things possible to do with the ServletContext instance, for example accessing WEB-INF resources(xml configs and etc.) by calling the getResourceAsStream() method.
Typically all application contexts defined in web.xml in a servlet Spring application are Web Application contexts, this goes both to the root webapp context and the servlet's app context.

Also, depending on web application context capabilities may make your application a little harder to test, and you may need to use MockServletContext class for testing.

Difference between servlet and root context
Spring allows you to build multilevel application context hierarchies, so the required bean will be fetched from the parent context if it's not present in the current application context. In web apps as default there are two hierarchy levels, root and servlet contexts: Servlet and root context.

This allows you to run some services as the singletons for the entire application (Spring Security beans and basic database access services typically reside here) and another as separated services in the corresponding servlets to avoid name clashes between beans. For example one servlet context will be serving the web pages and another will be implementing a stateless web service.

This two level separation comes out of the box when you use the spring servlet classes: to configure the root application context you should use context-param tag in your web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/root-context.xml
/WEB-INF/applicationContext-security.xml
</param-value>
</context-param>

(the root application context is created by ContextLoaderListener which is declared in web.xml

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

)
and servlet tag for the servlet application contexts

<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>app-servlet.xml</param-value>
</init-param>
</servlet>

Please note that if init-param will be omitted, then spring will use myservlet-servlet.xml in this example.

See also: Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

SpringMVC config applicationContext.xml should import some-servlet.xml

please change your spring bean config file user-servlet.xml name as SpringMVC-servlet.xml and remove init-param entries. And move it in WEB-INF or map resources folder as root folder in deployment assembly(for eclipse ide). It should work fine.

for UserService issue- check annotation on UserService class. It should be annotated with @Service or any other sterotype annotation and class must reside in componentScan declared package. Also User service must have a no arg default constructor or else you need to instantiate the dependent constructor arg of UserService class.

what is the difference between xml files Spring and Spring MVC framework

The servletname-servlet.xml defines the beans for one servlet's app context. There can be number of servlets in a webapp and for every servlet we have servletname-servlet.xml (e.g. spring1-servlet.xml for servlet1, spring2-servlet.xml for servlet2).

Beans defined in servletname-servlet.xml can reference beans in beans.xml, but not vice versa.

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

Beans.xml contain beans that are shared between all servlets in a webapp.Usually the beans.xml context is not necessary if you have only one servlet in your webapp.



Related Topics



Leave a reply



Submit