八、Spring 配置IOC工厂避免多次创建

在前面的文章中,我们初始化Spring的时候采用的都是类似下面的几行代码:

	// 创建Spring的工厂类:
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	// 通过工厂解析XML获取Bean的实例,这里传入的是id
	UserService us1 = (UserService) applicationContext.getBean("userService");
	us1.sayHello();

每次执行的时候都会看到这些初始化的log
在这里插入图片描述
假设我们的代码是写在请求方法里面,客户端每请求一次都会重新初始化一次IOC工厂,而每次创建工厂都会将Spring配置文件中注册所有类进行初始化,这样必然会消耗更多的内存资源,也影响性能。

Spring提供的解决方案就是通过监听ServletContext的生命周期,在其创建的时候进行IOC工厂的初始化,由于ServletContext的生命周期是最长的,当项目启动的时候,服务器为每一个web项目创建一个servletcontext对象.当项目被移除的时候或者服务器关闭的时候servletcontext才销毁,这就完美的解决了Spring IOC工厂仅初始化一次的需求。

下面介绍操作步骤
1.首先需要引入Spring提供的web包。
spring-web-4.2.4.RELEASE.jar
完整路径:\spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\libs\spring-web-4.2.4.RELEASE.jar
在这里插入图片描述
该jar包已经封装好了一个监听ServletContext的生命周期ServletContextListener的监听器对象,对于监听器不了解的可以先看这篇文章java web域对象的监听器

Spring提供的ServletContextListener监听器全路径类名是org.springframework.web.context.ContextLoaderListener
查看源码可以发现,它是实现了ServletContextListener接口的.
在这里插入图片描述
2.在web.xml中注册Spring监听器
既然有了监听器了,那么就需要在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">

	<!-- 注册ServletContextListener监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 设置加载Spring IOC工厂的配置文件路径 -->
	<context-param>
		<!-- 参数名称固定 -->
		<param-name>contextConfigLocation</param-name>
		
		<!-- 默认只能加载WEB-INF目录下的配置文件,通过下面的值就可以指定加载src路径下的配置文件了  -->
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
</web-app>

这样配置后,只要项目一部署,IOC工厂就自动初始化完毕了

3.如何获取Spring框架已经初始化好的IOC工厂呢?

需要使用Spring-web jar包提供的工具类WebApplicationContextUtils,以Servlet使用为例,具体代码如下:

package blog.csdn.net.mchenys.web;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import blog.csdn.net.mchenys.domain.User;

/**
 * 测试Spring IOC工厂
 */
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

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

		// 获取Spring IOC工厂,这里需要传入一个ServletContext对象
		ServletContext servletContext = request.getServletContext();
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

		// 然后就可以使用工厂获取bean对象了
		User user = (User) ctx.getBean("user");
		System.out.println(user);
	}

}

当项目部署后,控制台输出的log中会看到Spring IOC工厂已经初始化了.
在这里插入图片描述
有的人可能会好奇为什么我的项目,Spring加载配置文件的log有多条, 那是因为我在写前面文章的时候,做过一个配置文件分开管理的测试,具体可以看这篇文章七、Spring框架的配置文件分开管理

当浏览器地址输入http://localhost:8080/Spring01/myServlet进行请求时,控制台不再输出任何关于Spring IOC工厂的初始化log信息了,只看到打印的User对象的toString内容.多次请求也是一样.
在这里插入图片描述

如果你的测试类是Struts2的Action,前面的操作还是一样,只是获取ServletContext 的方式变成了通过ServletActionContext.getServletContext()来获取而已,其它完全一样.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值