Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
How to access ApplicationContext (Bean-Factory) of Spring in Web Servlet with Example
3367 Views
Hints 
Below is an example of "How to access ApplicationContext (Bean-Factory) of Spring in Web Servlet with Example".
Step.1 Start a Java Project with required jars 
  1. Select New menu -> Dynamic Web Project
  2. Enter Project Name as "ApplicationContextAccessInWebServletExample"
  3. Click Next, Selecting Target runtime as Apache Tomcat 7.0
  4. Click Next, Check Generate web.xml deployment descriptor then click on "Finish"
  5. Copy and paste Spring's 21 Framework Jars and commons-logging-1.1.jar into /WEB-INF/lib
Step.2 Project Explorer Preview  
RndHolder.java
package com.knowledgewalls;

import org.springframework.stereotype.Component;

@Component
public class RndHolder {
    private int rndNumber;
    
    public int getRndNumber(){
        return rndNumber;
    }
    public void setRndNumber(int rndNumber){
        this.rndNumber = rndNumber;
    }
    
    public void generateRndNumber(){
        this.rndNumber = (int) (Math.random() * 99999);
    }
}
ShowAccountDetails.java
package com.knowledgewalls;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;

@WebServlet("/ShowAccountDetails")
public class ShowAccountDetails extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(req.getServletContext());
            RndHolder rndHolder = (RndHolder) context.getBean("rndHolder");
            rndHolder.generateRndNumber();
            
        resp.getWriter().write("Rnd Number: "+rndHolder.getRndNumber());
    }
}
beans-servlet.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.knowledgewalls" />
</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RequestScopeExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
 
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/beans-servlet.xml </param-value>
  </context-param>
</web-app>
Output 
Hints. Run ShowAccountDetails.java (Run on Server)

Output: 01

Output: 02
Download as Zip 
Link to download
ApplicationContextAccessInWebServletExample

Hints.
Click on File menu. then click "Download"
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details