发布于 2016-07-03 05:31:27 | 169 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


这篇文章主要为大家详细介绍了监听器获取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。

原文链接:http://blog.csdn.net/eson_15/article/details/51373937

以上就是iPhone6splus微信闪退的解决方法,希望对大家有所帮助,也希望大家多多支持phperz,关注phperz的更多精彩内容。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务