监听器如何获取Spring配置文件


我们在做项目的时候,会用到监听器去获取Spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出来放到application里,即在监听器里调用后台商品业务逻辑的方法,也就是说我们需要在监听器里获取Spring中配置的相应的bean。先把监听器创建出来:

1. 创建InitDataListener

        创建一个监听器InitDataListener继承ServletContextListener:

/**
 * @Description: TODO(用于项目启动的时候数据初始化)
 * @author eson_15
 *
 */
//@Component //监听器是web层的组件,它是tomcat实例化的,不是Spring实例化的。不能放到Spring中
public class InitDataListener implements ServletContextListener {
	
	private ProductService productService = null;//productService中定义了跟商品相关的业务逻辑
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {

	}

}

并在web.xml中配置该监听器:

        如上,productService中定义了商品的一些业务逻辑,并且这个productService是交给Spring管理的,那么我们如何得到这个对象呢?首先肯定的一点是:我们不能自己new出来,因为new出来的话就跟Spring的IoC没有关系了……主要有三种方式可以实现,我们先一个个分析,最后比较优劣。

2. 直接加载beans.xml文件

        这种方式比较简单粗暴,不是要加载配置文件么?那好,我加载就是了,如下:

//@Component //监听器是web层的组件,它是tomcat实例化的,不是Spring实例化的。不能放到Spring中
public class InitDataListener implements ServletContextListener {

	
	private ProductService productService = null; //productService中定义了跟商品相关的业务逻辑
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		// 获取业务逻辑类productService查询商品信息		
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		productService = (ProductService) context.getBean("productService");
		System.out.println(productService); //输出看看拿到了没有		

                //下面是具体productService相关操作……
	}

}

      这种方法完全没问题,思路很清晰,先加载配置文件beans.xml,然后获取bean,但是启动tomcat后,我们看看控制台输出的信息:

        到这里应该发现这种方式的弊端了,加载了两次配置文件,也就是说那些bean被实例化了两次,从打印的信息来看,是拿到我们自己加载配置文件是实例化的bean。这种方式明显不可取。

3. 从ServletContext中获取

        从上面的方法中,我们最起码可以知道,Spring通过自己的监听器已经加载过一次配置文件了,我们没必要再加载一次,那么很容易想到,如果知道Spring加载后放到哪里了,那我们就可以从那地方获取该配置文件,下面我们看下Spring加载配置文件的过程:

        上图中(省略了无关的代码),ContextLoaderListener就是web.xml中我们配置的Spring监听器,它也实现了ServletContextListener并继承了ContextLoader。在监听器中主要通过initWebApplicationContext方法来获取配置文件,并创建WebApplicationContext对象,在initWebApplicationContext方法里主要做两件事:一是拿到Spring的上下文,二是把Spring上下文放到ServletContext中,并且键为:WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。那么如何拿到Spring的上下文呢?是通过获取web.xml中配置的Spring的路径,CONFIG_LOCATION_PARM其实是个字符串常量,就是上面web.xml中配置Spring监听器下面的:

<context-param>
      <param-name>contextConfigLocation</param-name> <!--CONFIG_LOCATION_PARM就是contextConfigLocation-->
      <param-value>classpath:beans.xml</param-value>
</context-param>

        所以就很明显了,通过web.xml中配置的路径拿到beans.xml,然后加载这个配置文件,实例化bean。

        现在我们既然知道了Spring在加载配置文件后,把它放在了ServletContext中,那么我们就可以去这里面直接拿!

//@Component //监听器是web层的组件,它是tomcat实例化的,不是Spring实例化的。不能放到Spring中
public class InitDataListener implements ServletContextListener {

	
	private ProductService productService = null;
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		//  获取业务逻辑类查询商品信息
		
		// 解决方案二,项目在启动时,把Spring配置文件通过Spring的监听器加载,存储到ServletContext中,我们只要在ServletContext中获取即可。
		ApplicationContext context = (ApplicationContext) event.getServletContext()
                                             .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		productService = (ProductService) context.getBean("productService");
		System.out.println(productService);		
	}

}

        这样我们就可以拿到produceService的实例化对象了,这种方法好是好,就是getAttribute中的参数太长,也不知道当时程序员的脑门子被夹了还是咋地,估计是想不到其他更合适的名字了吧~

4. 通过Spring提供的工具类加载

        也许开发Spring的大牛们也意识到了这个参数名字太长了,于是他们提供了一个方法类,可以加载配置文件:

public class InitDataListener implements ServletContextListener {

	
	private ProductService productService = null;
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		// 获取业务逻辑类查询商品信息

		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		productService = (ProductService) context.getBean("productService");
		System.out.println(productService);
	}

}

        其实,这里的getWebApplicationContext方法就是把上面的那个方法封装了一下而已,我们看看这个方法的源码就知道了:

public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
		return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	}

        这样更加方便程序员调用,仅此而已……所以一般我们使用第三种方法来获取Spring的配置文件,从而获取相应的实例化bean。

Spring中如何配置Hibernate事务

Struts2整合Spring方法及原理

基于 Spring 设计并实现 RESTful Web Services

Spring-3.2.4 + Quartz-2.2.0集成实例

使用 Spring 进行单元测试

运用Spring注解实现Netty服务器端UDP应用程序

Spring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码

Spring 的详细介绍:请点这里
Spring 的下载地址:请点这里

本文永久更新链接地址

相关内容

    暂无相关文章