Learn the basic Web chapter of Spring together

Summary

In the daily development, it is more and more important for web project to integrate Spring framework, and Spring framework has become one of the mainstream frameworks of web development. This article focuses on the simple use of Spring framework for Java development web project integration, and the differences between using Spring and not using Spring framework It is only for learning and sharing. Please correct any deficiencies.

Page access flowchart

The flow chart of page access in this example is as follows:

 

Development process without Spring framework

The steps are as follows:

1. Add class and interface implementation corresponding to Service and Dao

As follows: Dao is strongly associated in the Service

 1 package com.hex.Dao;
 2 
 3 /**
 4  * Student Dao
 5  * @author Administrator
 6  *
 7  */
 8 public interface IStudentDao {
 9     public String GetStudentById(int id);
10 }
11 ////////////////////////////////////////
12 package com.hex.Dao;
13 
14 /**
15  * Student Dao
16  * @author Administrator
17  *
18  */
19 public class StudentDaoImpl implements IStudentDao {
20 
21     /**
22      * Query student information
23      */
24     @Override
25     public String GetStudentById(int id) {
26         
27         return "hex";
28     }
29 
30 }
31 ////////////////////////////////////////
32 package com.hex.Service;
33 
34 /**
35  * Student service interface
36  * @author Administrator
37  *
38  */
39 public interface IStudentService {
40     public String GetStudentById(int id);
41 }
42 ////////////////////////////////////////
43 package com.hex.Service;
44 
45 import com.hex.Dao.IStudentDao;
46 import com.hex.Dao.StudentDaoImpl;
47 
48 /**
49  * Student service implementation class
50  * @author Administrator
51  *
52  */
53 public class StudentServiceImpl implements IStudentService {
54 
55     private IStudentDao studentDao;
56     
57     public void setStudentDao(IStudentDao studentDao) {
58         this.studentDao = studentDao;
59     }
60 
61     @Override
62     public String GetStudentById(int id) {
63         //studentDao=new StudentDaoImpl();
64         return studentDao.GetStudentById(id);
65     }
66 
67 }

2. Add a HomeServlet class and declare the studentService object through new

As follows:

 1 package com.hex.servlet;
 2 
15 /**
16  * Access Servlet implementation class
17  */
18 public class HomeServlet extends HttpServlet {
19     private static final long serialVersionUID = 1L;
20     
21     private IStudentService studentService;
22       
23 
24     /**
25      * Constructor26      */
27     public HomeServlet() {
28        
29     }
30     
31     /**
32      * Declare studentService object on initialization
33      */
34     @Override
35     public void init() throws ServletException {
36         studentService=new StudentServiceImpl();
37     }
38 
39     /**
40      * Get Method
41      */
42     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43         
44         String studentName=studentService.GetStudentById(0);
45         request.setAttribute("studentName", studentName);
46         request.getRequestDispatcher("/jsp/Home.jsp").forward(request, response);
47     }
48 
49     /**
50      * Post Method, the same as Get method
51      */
52     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
53         // TODO Auto-generated method stub
54         doGet(request, response);
55     }
56 
57 }

3. Access the front page

As follows:

1 <a href="../HomeServlet">Click to enter</a>

4. disadvantages:

There is a strong dependency that HomeServlet requires StudentServiceImpl objects. And StudentServiceImpl needs the support of StudentDao.

Spring access

1. You need to configure the listener corresponding to Spring in the web.xml file

As follows:

applicationContext.xml is located in the src directory, so you need to add classpath, which is the configuration file of Spring container

1 <context-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:applicationContext.xml
4     </param-value>
5 </context-param>
6 <!-- To configure spring-web.jar Corresponding monitor, Tomcat Automatic initialization at startup IOC container -->
7 <listener>
8     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
9 </listener>

2. Configure Spring's IOC container

As shown below: the attribute of the dependent reference object adopts ref mode, and if it is a value object, it adopts value mode.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xsi:schemaLocation="http://www.springframework.org/schema/beans
 5  http://www.springframework.org/schema/beans/spring-beans.xsd">
 6      <!-- Dao Depending on the underlying operation of the database, this example does not go deep -->
 7      <bean id="studentDao" class="com.hex.Dao.StudentDaoImpl"></bean>
 8      <!-- Service Layer dependent StudentDao,Use set Method injection -->
 9      <bean id="studentService" class="com.hex.Service.StudentServiceImpl">
10          <property name="studentDao" ref="studentDao"></property>
11      </bean>
12 </beans>

3. In the Servlet, the ApplicationContext object is introduced to associate the Tomcat container with the Spring IOC container

As shown below: other methods remain unchanged, add getter and setter methods of studentService object, and then generate the object through container injection declaration. The dependency on StudengDao in StudentServiceImpl is injected in the same way.

 1 package com.hex.servlet;
 2 
 3 /**
 4  * Servlet Implementation class
 5  */
 6 public class HomeServlet extends HttpServlet {
 7     private static final long serialVersionUID = 1L;
 8     
 9     private IStudentService studentService;
10        
11     public IStudentService getStudentService() {
12         return studentService;
13     }
14 
15     public void setStudentService(IStudentService studentService) {
16         this.studentService = studentService;
17     }
18     
19     /**
20      * Get bean object in IOC container of spring during initialization
21      */
22     @Override
23     public void init() throws ServletException {
24         //Web Project acquisition Spring Context object.
25         ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
26         studentService=(IStudentService)context.getBean("studentService");
27     }
28 }

4. advantage:

In this way, the understanding and coupling between Servlet, Service and Dao are realized, and the flexibility and scalability are greatly enhanced.

Little knowledge

If there are multiple IOC container files of Spring, you can Import them as follows:

1 <! -- the second way is to import other container files -- >
2 <import resource="applicationContext2.xml"/>

How to configure servlet in web.xml is as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3     <display-name>FirstWebSpring</display-name>
 4     <servlet>
 5         <description>
 6         </description>
 7         <display-name>HomeServlet</display-name>
 8         <servlet-name>HomeServlet</servlet-name>
 9         <servlet-class>com.hex.servlet.HomeServlet</servlet-class>
10     </servlet>
11     <servlet-mapping>
12         <servlet-name>HomeServlet</servlet-name>
13         <url-pattern>/HomeServlet</url-pattern>
14     </servlet-mapping>
15     <welcome-file-list>
16         <welcome-file>index.html</welcome-file>
17         <welcome-file>index.htm</welcome-file>
18         <welcome-file>index.jsp</welcome-file>
19         <welcome-file>default.html</welcome-file>
20         <welcome-file>default.htm</welcome-file>
21         <welcome-file>default.jsp</welcome-file>
22     </welcome-file-list>
23     <!-- Configure container address -->
24     <!-- The first method is to load multiple configuration files, which can be written as follows:
25         <param-value>
26             classpath:applicationContext.xml,
27             classpath:applicationContext2.xml
28         </param-value>
29      -->
30     <context-param>
31         <param-name>contextConfigLocation</param-name>
32         <param-value>
33             classpath:applicationContext.xml
34         </param-value>
35     </context-param>
36     <!-- To configure spring-web.jar Corresponding monitor, Tomcat Automatic initialization at startup IOC container -->
37     <listener>
38         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
39     </listener>
40 </web-app>

Remarks

The rope saws the wood and the water drips through the stone.

Tags: Java Spring xml JSP

Posted on Sat, 09 Nov 2019 02:47:15 -0800 by stirton