Data reading difference between normal Java project and web project configuration file
- When reading data from the SpringIOC container in an ordinary Java project, you only need a new ClassPathXmlApplicationContext object to read the file, and you can read it wherever you need it, as shown below:
- In the web project, there are multiple page call background data locations. If there is no unified way to read, you need to write repeated code for many times, which is not easy to maintain. Therefore, in the web project, you can read it at one time in the init() method in the Servlet, and you need to cooperate with the listener.
Jar package needs to be prepared (jar package version is customized):
- Basic package:
- spring-aop-4.3.9.RELEASE
- spring-beans-4.3.9.RELEASE
- spring-core-4.3.9.RELEASE
- spring-context-4.3.9.RELEASE
- spring-expression-4.3.9.RELEASE
- spring-web-4.3.9.RELEASE
- commons-logging-1.1.1
web.xml file configuration
<?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_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- initialization springIOC Container( applicationContext.xml),Must tell springIOC Location of container--> <!-- monitor ContextLoaderListener Parent class of ContextLoader About finding SpringIOC Properties for container location contextConfigLocation --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- to configure Spring-web.jar Provides a listener that can be initialized when the service is started springIOC container --> <listener>org.springframework.web.context.ContextLoaderListener</listener> </web-app>
- Explanation: for a web project, it is impossible to initialize the container to fetch data every time the data in the container is used, resulting in too much code redundancy. A normal web project needs to configure a listener in web.xml and use the listener provided in Spring-web.jar to monitor whether the project is started. When the project is started, the listener will load the web.xml file and then initialize SPR The springIOC container is really started at one time, and all the properties in the springIOC container are initialized.
- There are two ways to initialize a container through a web.xml file:
-The first kind of applicationContext.xml file is in the src classpath. The listener attribute and context param attribute need to be used together. The listener attribute is used to configure the listener, and the context param attribute is used to inform the listener of the location of the applicationContext.xml file to be loaded. The parent class ContextLoader of the listener ContextLoaderListener has information about finding the SpringIOC container bit In the first method, the file name of applicationContext.xml can be changed at will. Just change the file name in param value attribute in context param attribute in the same period.
-The second is the default configuration. The applicationContext.xml file must be located at the same level of the web.xml file, that is, under the WEB-INF directory. If the default configuration is performed, only the listener attribute is required, and the context param attribute is not required. It must be noted that the location of the applicationContext.xml file is at the same level of the web.xml file, and the name can only be applicationContext.xml, not It is not enough to make changes, otherwise the startup fails.
Splitting the configuration file of Spring development web project
There are usually two ways to split the configuration file applicationContext.xml:
- 1. Split according to three layers (UI layer, Service layer and DAO layer), such as applicationContextController.xml, applicationContextService.xml, applicationContextDao.xml, etc;
- 2. Split according to functions, such as applicationContextStudent.xml, applicationContextCass.xml, etc. each function corresponds to an applicationContext.xml file.
Loading methods of multiple different applicationContext.xml files at the same time:
-
Synchronization configuration of multiple files (as shown below)
-
Use wildcards for configuration (as shown below)
-
Use the applicationContext.xml file to load other files
Configure only the applicationContext.xml file in the context param attribute, and then import other files to be loaded in the applicationContext.xml file.
###Bridge between web.xml and applicationContext.xml -
The web.xml configuration is as follows
<?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_2_5.xsd 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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>StudentServlet</servlet-name> <servlet-class>org.dsl.Servlet.StudentServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StudentServlet</servlet-name> <url-pattern>/StudentServlet</url-pattern> </servlet-mapping> </web-app>
- In the web project, when the front page is triggered, the data is obtained directly from the tomcat container, and the data we really return is in the SpringIOC container. The two are not the same container. If they want to be unified, they must establish a connection between them, and there is a point to point relationship between the two, that is, calling the service layer in the tomcat container, then it is necessary. The service in the SpringIOC container is invoked with the service layer in the tomcat container. The last operation is to get the data from the SpringIOC container in the inti method in Servlet.
- There are two ways to obtain the data in the SpringIOC container in the init method:
- 1. For the first, you can still use the ClassPathXmlApplicationContext object to read the file, but the parent class is the ClassPathXmlApplicationContext object of ApplicationContext, and the referenced package is as follows:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
- Second, the method provided in spring-web.jar is used to automatically match the corresponding applicationContext.xml file. The specific statements are as follows:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
- The example code is as follows
package org.dsl.Servlet; 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.dsl.service.StudentService; import org.dsl.service.impl.StudentServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * Servlet implementation class StudentServlet */ public class StudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; private StudentService studentService; @Override public void init() throws ServletException { //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); studentService= (StudentService) context.getBean("studentService"); } public void setStudentService(StudentService studentService) { this.studentService = studentService; } /** * @see HttpServlet#HttpServlet() */ public StudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String nameString = studentService.queryName(); request.setAttribute("nameString", nameString); request.getRequestDispatcher("result.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
- Note: the first method can be executed without configuring the listener and find out the results; however, the second method is really based on web projects, so the listener must be configured.