普通Java项目与web项目配置文件中数据读取差异
- 普通Java项目中从SpringIOC容器中读取数据时,只需new一个ClassPathXmlApplicationContext对象读取文件即可,哪里需要就在哪里进行读取即可,如下所示:
- 在web项目中存在多处页面调用后台数据位置,如果没有统一的方式读取,则需要进行多次重复代码的编写不易维护,所以在web项目中可以在Servlet中的init()方法中统一一次性的读取,并且需要与监听器进行配合。
需要准备JAR包(Jar包版本自定):
- 基本包:
- 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文件配置
<?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>
<!-- 初始化springIOC容器(applicationContext.xml),必须告诉springIOC容器的位置-->
<!-- 监听器ContextLoaderListener的父类ContextLoader有关于查找SpringIOC容器位置的属性contextConfigLocation -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring-web.jar提供的监听器,此监听器可以在服务启动时,初始化springIOC容器 -->
<listener>org.springframework.web.context.ContextLoaderListener</listener>
</web-app>
- 解释:针对于web项目,不可能在每处用容器中的数据时,都要进行初始化容器取数,造成过多的代码冗余。正常的web项目需要在web.xml中配置监听器,利用Spring-web.jar中提供的监听器,进行监听项目是否启动,此监听器在项目启动时,会加载web.xml文件,然后初始化springIOC容器,真正做到了一次启动,springIOC容器中的属性全部初始化完成。
- 通过web.xml文件来初始化容器有两种方式:
- 第一种applicationContext.xml文件在src类路径下,需要listener属性和context-param属性一起配合使用,listener属性用来配置监听器,context-param属性用来通知监听器需要加载的applicationContext.xml文件的位置。其中监听器ContextLoaderListener的父类ContextLoader有关于查找SpringIOC容器位置的属性contextConfigLocation。使用第一种方式,applicationContext.xml文件文件名可以随意变化,只需要将context-param属性中param-value属性中文件名称,同期变化即可。
- 第二种是默认配置,applicationContext.xml文件必须位于web.xml文件的同一级,即WEB-INF目录下。如果进行默认配置,则只需要listener属性即可,无需配置context-param属性。必须注意applicationContext.xml文件的位置在web.xml文件的同一级,并且名字只能是applicationContext.xml,不能够进行更改,否则启动失败。
Spring开发web项目配置文件的拆分
配置文件applicationContext.xml文件的拆分通常存在两种方式:
- 1.按照三层进行拆分(UI层、Service层、DAO层)例如:applicationContextController.xml、applicationContextService.xml、applicationContextDao.xml等;
- 2.按照功能去拆分,例如applicationContextStudent.xml、applicationContextCass.xml等,每个功能对应一个applicationContext.xml文件。
多个不同的applicationContext.xml文件同时加载方式:
-
多个文件同期配置(如下图)
-
使用通配符进行配置(如下图)
-
利用applicationContext.xml文件,加载其他文件
在context-param属性中只配置applicationContext.xml文件,然后在applicationContext.xml文件中import导入其他需要加载的文件。
###web.xml与applicationContext.xml的桥梁 -
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_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>
- 在web项目中,前端页面被触发时,是直接从tomcat的容器中直接获取数据的,而我们真正返回的数据是在SpringIOC容器中的,两者并不是同一个容器,想要两者统一,必须在两者之间建立联系,并且两者之间是点对点的联系,即在tomcat的容器中调用service层,那么就需要将SpringIOC容器中的service与tomcat的容器中调用service层进行关联。最后操作就是在Servlet中的inti方法中将SpringIOC容器中的数据获取到。
- 在init方法中获取SpringIOC容器中的数据存在两种方式:
- 1第一种,仍然利用ClassPathXmlApplicationContext对象读取文件即可,但是父类是ApplicationContext的ClassPathXmlApplicationContext对象,所引用的包是下面的:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
- 第二种,利用spring-web.jar中提供的方法,来自动匹配相应的applicationContext.xml文件,具体语句如下:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
- 示例代码如下
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);
}
}
- 注:第一种方式不需要配置监听器也可以执行,并查出结果;但是第二种是真正基于web项目的,所以必须配置监听器。