In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader.
ContextLoaderListener is simple way to use the spring in web application. this listener accept contextConfigLocation parameter from context-parama.
You have to enter following code in web.xml file
define contextConfigLocation parameter in context-param tag.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/services.xml</param-value>
</context-param>
services.xml is the Spring configuration file in which you define beans.
Add the ContextLoaderListener listener.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Here is the complete source code for the example demonstrating this.
import java.util.Random;
import java.util.Scanner;
public class InputService {
public int getIntValue() {
Random r=new Random();
int val=r.nextInt();
System.out.println("Returning value = "+val);
return val;
}
}
OutputService.java
public class OutputService{
public int write(int x) {
System.out.println("Output is "+x);
return x;
}
}
CalServlet .java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class CalServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
CalcMachine cal = (CalcMachine) ac.getBean("cal");
response.getWriter().print("Adding of numbers is = " + cal.doAdd());
response.getWriter().close();
}
}
services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="in" class="InputService"/>
<bean id="out" class="OutputService"/>
<bean id="cal" class="CalcMachine">
<property name="inputService">
<ref bean="in" />
</property>
<property name="outputService">
<ref bean="out" />
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>DI2_web</display-name>
<servlet>
<description></description>
<display-name>CalServlet</display-name>
<servlet-name>CalServlet</servlet-name>
<servlet-class>CalServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CalServlet</servlet-name>
<url-pattern>/CalServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/services.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
- ContextLoaderListener : It is listener implementation that is added to web.xml file.
- ContextLoaderServlet : It is servlet implementation that is configured with load-on-startup tag in web.xml.
ContextLoaderListener is simple way to use the spring in web application. this listener accept contextConfigLocation parameter from context-parama.
You have to enter following code in web.xml file
define contextConfigLocation parameter in context-param tag.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/services.xml</param-value>
</context-param>
Add the ContextLoaderListener listener.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Here is the complete source code for the example demonstrating this.
InputService.java
import java.util.Scanner;
public class InputService {
public int getIntValue() {
Random r=new Random();
int val=r.nextInt();
System.out.println("Returning value = "+val);
return val;
}
}
OutputService.java
public class OutputService{
public int write(int x) {
System.out.println("Output is "+x);
return x;
}
}
CalServlet .java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class CalServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
CalcMachine cal = (CalcMachine) ac.getBean("cal");
response.getWriter().print("Adding of numbers is = " + cal.doAdd());
response.getWriter().close();
}
}
services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="in" class="InputService"/>
<bean id="out" class="OutputService"/>
<bean id="cal" class="CalcMachine">
<property name="inputService">
<ref bean="in" />
</property>
<property name="outputService">
<ref bean="out" />
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>DI2_web</display-name>
<servlet>
<description></description>
<display-name>CalServlet</display-name>
<servlet-name>CalServlet</servlet-name>
<servlet-class>CalServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CalServlet</servlet-name>
<url-pattern>/CalServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/services.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Comments