有时,我们不通过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. solr学习一(一大堆的学习资料)

    这篇博客含有海量资料,学习solr必备字典 大概看完solr就基本上手了. solr原味资料:http://wiki.apache.org/solr/FrontPage (     http://wi ...

  2. tomcat源码阅读之Catalina和Bootstrap解析

    一.Cataling类分析: 1.Catalina类是启动类,用于启动或者关闭Server对象,它包含一个Digester对象,用于解析tomcat配置文件:conf/server.xml;调用pro ...

  3. tomcat源码阅读之单点登录

    一.SSO概念: 单点登录,Single Sign-On,简写为 SSO,是一个用户认证的过程,允许用户一次性进行认证后,就可访问系统中不同的应用:而无需要访问每个应用时,都重新输入用户和密码. 实现 ...

  4. Ionic slides 轮播图

    1. 创建界面 <ion-content> <ion-slides pager class="myslides"> <ion-slide> &l ...

  5. Linux(Centos7)下安装 zookeeper docker版 集群

    为了省去麻烦的软件安装,现在开发环境需要的软件越来越习惯于docker安装了,先看下安装后的截图,开发环境正在启动的容器 1.首先系统需要先支持docker …… 由于之前安装几次都没有做流程记录,在 ...

  6. 【python】专用下划线标识符说明

    __xxx__:系统定义名字 __xxx:类中私有变量名 说明:__xxx看做“私有的”,在模块或者类外是不可以使用.

  7. debian下配置keepalived ha

    抄袭自http://blog.51yip.com/server/1417.html,做了一些修改 可以参考http://blog.linuxphp.org/archives/1615/ 备注:NAT模 ...

  8. samba 挂载windows共享文件夹

    先转载一片文章     centOS下yum安装配置samba 地址 http://blog.csdn.net/linglongwunv/article/details/5212875 遇到问题1 # ...

  9. vue 感觉很好的渲染模式

    <ul v-if="todos.length"> <li v-for="todo in todos"> {{ todo }} </ ...

  10. Qt5布局管理(一)——QSplitter分割窗口类

    转载:LeeHDsniper 概述 本文首先通过三个实例分别介绍Qt5的分割窗口QSplitter类.停靠窗口QDockWidget类.堆栈窗体QStackedWidget类,然后介绍布局管理器的使用 ...