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

Analysis

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:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!--    spring Core container-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <!--      spring-web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>


        <!--web Dependent-->
        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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="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.xsd">
<!--    test bean-->
    <bean id="integer" class="java.lang.Integer">
        <constructor-arg type="java.lang.String" value="100"/>
    </bean>
</beans>

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

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<!--  spring configuration file:-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<!--  spring Listener enables container to start with project:-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

</web-app>

Test Servlet

package com.yh.servlet;


import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

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 java.io.IOException;

@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