Using Spring in common WEB projects

Spring is an object container to help us manage the objects in the project. Which objects in the web project should be managed by spring?

Objects involved in the project

Let's review the objects involved in the WEB project

  • Servlet
  • Request
  • Response
  • Session
  • Service
  • DAO
  • POJO


When we learn IOC containers, we know that Spring can help us manage the objects that need to be created and managed by ourselves. If an object doesn't need to manage its creation and declaration cycle, it doesn't need to be handed over to Spring for management

From this point of view, only Service, DAO and POJO in the above objects should be handed over to Spring for management. Since POJO is usually created dynamically by the persistence layer framework, only Service and DAO should be handed over to Spring container for management,

Of course, AOP in Spring is also a very common function, such as transaction management, log output, etc

Minimum pom dependency:


        <!--    spring Core container-->
        <!--      spring-web-->

        <!--web Dependent-->
        <!-- -->
        <!-- -->
        <!-- -->

The above dependencies can ensure that the project can use the Spring container and the normal use of JSP, El and servlet. After the test is completed, the project can be developed like a normal Spring or web project

Spring configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<!--    test bean-->
    <bean id="integer" class="java.lang.Integer">
        <constructor-arg type="java.lang.String" value="100"/>

An integer was added to the container to test whether the container is available


 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

<web-app version="2.5"

  <display-name>Archetype Created Web Application</display-name>

<!--  spring configuration file:-->
<!--  spring Listener enables container to start with project:-->


Test Servlet

package com.yh.servlet;

import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "TestServlet",urlPatterns = "/test")
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //Get Spring container
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        //Get object tests in container
        Object integer = context.getBean("integer");
        response.getWriter().println("hello spring "+integer);

Configure tomcat to start access: http: / / localhost: 8080 / hmwk? War? Expanded / test to view the integer 100 in the container, indicating that Spring and WEB projects are working normally!

Keywords: Java Spring JSP xml

Added by theoph on Tue, 04 Feb 2020 08:17:18 +0200