在用到spring的项目中往往需要获取到ApplicationContext,网上随便一搜也有很多,无非就是以下五种:
- ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
-
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); - 继承自抽象类ApplicationObjectSupport
- 继承自抽象类WebApplicationObjectSupport
- 实现接口ApplicationContextAware
其实有一种更好更方便的方法,那就是
WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext();
这种方法与上面后三种相比好处就不用多说了,与第一种相比,不需要重新加载一次xml配置文件,节省资源和时间。与第二种相比不仅不需要ServletContext ,速度上也有明显优势,几乎不需要消耗时间。亲们可以尝试下!
相关推荐
主要介绍了 Spring获取ApplicationContext对象工具类的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Spring获取webapplicationcontext,applicationcontext几种方法详解
今天小编就为大家分享一篇关于Spring通过ApplicationContext主动获取bean的方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/368874
在Service端,自定义ApplicationContext,获取Spring容器。从而,通过编码的形式,手动获取注册到容器中的对象。
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件4
//创建spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 ...
SpringUtil工具类(获取applicationContext,通过name获取 Bean,通过class获取Bean,通过name,以及Clazz返回)
12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...
3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...
4.测试:启动Spring,获取Hello示例。 二、Spring基于XML装配实验 说明:使用Spring IOC模拟实现账户添加功能程序 1.创建JavaBean类:Account.java 2.创建DAO类: AccountDao.java,模拟账户添加操作 3.创建Service...
c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts ...
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入<bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /> 即可使用
6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...
3.5.2 ApplicationContext中Bean的生命周期 3.6 小结 第4章 在IoC容器中装配Bean 4.1 Spring配置概述 4.1.1 Spring容器高层视图 4.1.2 基于XML的配置 4.2 Bean基本配置 4.2.1 装配一个Bean 4.2.2 Bean的命名 4.3 ...
Spring源代码解析,包括: ApplicationContext源代码解析,Spring声明式事务管理之事务开始,Spring声明式事务管理之事务提交,IOC容器,IoC容器在Web容器中的启动,Spring JDBC,Spring MVC,Spring AOP获取Proxy,...
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
1.1 ApplicationContext应用上下文获取方式 1.2 Spring提供获取应用上下文的工具 1.4 配置ContextLoaderListen