`

项目启动时,保存Spring的ApplicationContext实例

阅读更多

         今天遇到一个问题就是要实例化对象的一些对象的时候,里面需要用到数据库操作,所以里面有dao层的引用,但是这些要创建的对象,使用的参数都不一定一样的,所以在这些对象里我需要有一个ApplicationContext的引用来获取相应的资源对象。网上找了一下,找到了处理的方法,记录一下。为了避免多次加载或者获取ApplicationContext对象的麻烦,这个时候我们应该在项目启动的时候就把系统启动时的创建的那个ApplicationContext的实例保存起来,用一个静态变量保存,方便以后使用。做法就是继承ContextLoaderListener类,并重写contextInitialized方法,从里面取得ApplicationContext对象并保存。代码如下:

 

public class MyContextLoaderListener extends ContextLoaderListener {
	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext context = event.getServletContext(); 
        super.contextInitialized(event); 
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context); 
        SystemApplicationContext.setContext(ctx);
	}
}

 

      然后修改web.xml文件,使用自己的applicaitonContext对象

 

<listener>
		<listener-class>com.cdj.test.MyContextLoaderListener</listener-class>
</listener>

 

      需要使用ApplicationContext对象的时候只要使用SystemApplicationContext.getContext();方法即可。

分享到:
评论

相关推荐

    Spring MVC开发配置文件 applicationContext

    Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext

    spring配置实例化后执行的初始化方法比对

    spring配置对象实例化后执行的方法,两种实现比对,通过InitializingBean接口,并实现arterPropertiesSet()方法;或者通过applicationContext.xml配置init-method属性

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    spring配置实例化后执行的销毁方法比对

    spring配置对象实例化后执行的方法,两种实现比对,通过DisposableBean接口,并实现destory()方法;或者通过applicationContext.xml配置destory-method属性

    Spring MVC 入门实例

    这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例 spring jar 包详解spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下...

    基于java的企业级应用开发:Spring的核心容器.ppt

    Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; ...

    Spring框架xml注解配置方式实例

    Spring框架xml注解配置方式实例,包括Maven配置文件pom.xml、Springxml配置文件ApplicationContext-1.xml,以及类源码

    springweb3.0MVC注解(附实例)

    -- Spring 容器启动监听器 --&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC...

    Spring Security 新手入门级maven实例

    官方spring-security-samples-tutorial 实例;maven工程 导入后工程科直接运行。角色账户放于applicationContext-security.xml

    Struts2+Spring3+Hibernate3 用户管理系统实例源码

    本例主要是实现了struts2+spring3+hibernate3的 基本框架搭建的注册登录,以及用户增删改查,适于初学者学习。 包括:注册 登录功能 分页的实现 前端校验 验证码的实现 注册时有ajax 校验,登录时 后台从数据库...

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...

    Spring MVC 框架应用实例

    /WEB-INF/database.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;encodingFilter org.springframework.web.filter....

    spring技术入门相关源码

    //创建spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 ...

    spring源代码解析

    Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...

    GWT+Spring2+Spring Security2+Hibernate3实例

    一个GWT+Spring2+Spring Security2+Hibernate3的实例,数据库为mssql.在/GwtWeb/WebRoot/WEB-INF/lib 目录下RequiredJar.txt有需要用到的库说明 修改一下applicationContext.xml的数据库设定就可以使用

    spring 容器.docx

    Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口。他们都可代表Spring容器,Spring容器是生成Bean实例的工厂,并且管理容器中的Bean。 Bean是Spring管理的...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    spring,struts,hibernate整合开发实例

    3.数据库采用的是oracle10g,sid采用的是orcl,如果oracle安装是sid不是orcl,请修改本实例下面的applicationContext.xml中的连接池代码,将orcl改为自己的数据库sid 4.请将实例中的数据库驱动ojdbc6放到tomcat的...

    spring2.0学习的案例

    spring中的配置问题,包,实例,applicationContext.xml

Global site tag (gtag.js) - Google Analytics