有时,我们不通过Controller层进入Service层,比如同步数据,任务,以及文件上传共通Handler对文件处理后保存数据等都会由一个非Controller类调用Service。

这时候如果new Service会因为没有事务控制也没法打开jdbc连接,也不满足spring的bean管理。因此需要获取到spring创建的service。

ApplicationContext会在spring的扫描时加入进来。

获取方式:

需要创建一个类(例:ContextUtils)实现ApplicationContextAware接口。

会自动实现一个setApplicationContext的方法,spring在扫描完毕类后,调用ApplicationContextAware接口的实现类,自动传递applicationContext到这个类中。

创建一个自己的getBean方法获取自己想要的类。

public class ContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
} public static Object getBean(String strBeanName) {
return context.getBean(strBeanName);
} }

在dispatch-context.xml中加入自己写的ContextUtils类。

<!-- ContextUtils -->
<bean id="contextUtils" class="com.pccw.solutions.retire.common.utils.ContextUtils"></bean>

这样就可以在自己想要的任何位置获取Bean.

SpringMvc 获取ApplicationContext的更多相关文章

  1. springMVC 使用WebApplicationContext获取ApplicationContext对象

    主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...

  2. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

  3. Spring获取ApplicationContext

    在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...

  4. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

  5. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  6. Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

    转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContex ...

  7. Spring Boot 获取ApplicationContext

    package com.demo; import org.springframework.beans.BeansException; import org.springframework.contex ...

  8. springMvc获取servletContext

    springMvc获取servletContext WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebA ...

  9. spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)

    2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234    学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...

随机推荐

  1. C# 使用oledb 方式连接本地或者远程oracel 数据库的方式

    对于C# 进行oracle 数据库的开发来说使用oracle 提供的odp.net 方式是比较方便的,同时在性能以及兼容性也是比较好的 但是,对于不打算使用的,那么该如何使用oledb 进行连接 连接 ...

  2. sql serve 创建序列

    Oracle中有sequence的功能,SQL Server类似的功能使用Identity列实现,但是有很大的局限性. 在2012中,微软终于增加了 sequence 对象,功能和性能都有了很大的提高 ...

  3. word如何让单页变横向

    word作为图文排版用户最多的软件之一,其功能的强大自不必说,比如将某一页在版式排版上设置为横排方向.那么,应该如何才能设置为横排的纸张呢?请阅读下文! 工具/原料 Microsoft Office ...

  4. vue-cli 引入阿里巴巴字体图标:注意点

    vue-cli 引入阿里巴巴字体图标:注意点 下载的 iconfont.css 文件中: .iconfont { font-family:"iconfont" !important ...

  5. shell教程-001:shell简介 什么是shell,shell命令的两种执行方式

    Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的. Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解 ...

  6. C语言扩展动态内存报错:realloc(): invalid next size: 0x0000000002365010 ***

    晚上被这个内存扩展崩溃的问题折腾的有点崩溃,当答案揭晓的那一刻,恍然大悟,原来如此简单. 练习题目:输入一个字符串,根据字母进行排序,说白了就是一个简单的冒泡 #include <stdio.h ...

  7. 4G的添加

    (ZTE 中兴 ME3760_V2 ) 1. 复制myLte文件到相关目录,设置权限为755 2.修改openwrt1407/package/base-files/files/etc/rc.local ...

  8. 可视化mark

    待尝试研究的可视化组件及产品: 开源组件 商业组件 开源组件 zeppelin Caravel D3.js Flare talend(ETL) pentaho spagoBI NanoCubs Dyg ...

  9. make_heap()等函数的用法

    1.make_heap() make_heap()用于把一个可迭代容器变成一个堆,默认是大顶堆. 它有三个参数.第一个参数是指向开始元素的迭代器,第二个参数是指向最末尾元素的迭代器,第三个参数是les ...

  10. HUD 1175 连连看

    连连看 Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...