在正常情况下 spring管理的类可直接调用Ioc容器中的实例,但在一些特殊情况下(例如拦截器中获取dao实例),Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该Bean先获取Spring容器,

spring提供了ApplicationContextAware接口,容器会自动检索实现该接口的类进行spring上下文注入

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; import lombok.extern.slf4j.Slf4j; @Slf4j
@Component
public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
log.info("ApplicationContext config success");
} // 获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
} // 通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} // 通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} // 通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
} }

使用例子,拦截器中获取 redis缓存实例

public class AccessInterceptor implements HandlerInterceptor {

    private IStringCacheService cache = SpringUtil.getBean(IStringCacheService.class);

    ......
......
......
......
......
}

获取ApplicationContext进而获取Ioc实例方法的更多相关文章

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

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

  2. Spring获取ApplicationContext

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

  3. Servlet 获取 ApplicationContext

    一般使用Spring完成了注入,在Service或SpringMVC 中可以通过注解的形式来获取 Spring的已经注入的Spring的bean如下所示: @Resource(name = " ...

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

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

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

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

  6. Spring Boot 获取ApplicationContext

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

  7. spring mvc在Controller中获取ApplicationContext

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

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

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

  9. springboot获取applicationcontext

    使用springboot之前,我们通过ClassPathXmlApplicationContext加载spring xml配置文件来获取applicationcontext,使用springboot后 ...

随机推荐

  1. win10安装Navicat 12 for MySQL

    Navicat 下载地址: https://blog.csdn.net/u013600314/article/details/80605981 Navicat 连接Mysql 的方法:https:// ...

  2. visual studio python快捷键

    打开交互式窗口: alt + i 智能提示: ctrl + j, or alt + ->

  3. Tosca:键盘输入字符串

    这是方法之一

  4. Diffie-Hellman算法简介

    一.DH算法是一种密钥交换协议,它可以让双方在不泄漏密钥的情况下协商出一个密钥来. DH算法基于数学原理,比如小明和小红想要协商一个密钥,可以这么做: . 小明先选一个素数和一个底数,例如,素数p=, ...

  5. openresty开发系列30--openresty中使用http模块

    OpenResty默认没有提供Http客户端,需要使用第三方提供的插件 我们可以从github上搜索相应的客户端,比如https://github.com/pintsized/lua-resty-ht ...

  6. django实战总结2

    https://www.jianshu.com/p/9b3bfe934511 https://www.cnblogs.com/1Q84mi/p/xadmin002.html https://blog. ...

  7. libfacedetection环境配置

    E:\Opencv\libfacedetection_install1\include E:\Opencv\libfacedetection_install1\lib libfacedetect-x6 ...

  8. 今天被这个BDE错误搞了半天,不过终于好了,分享一下

    今天正编译程序时,突然就报了这个错误出来,重启电脑都没用,多亏网上高手指教,先把解决方案列于下,供受此累得朋友查阅,自己也留底供查找:"Shared memory conflict ($21 ...

  9. 【kubectl 原理】kubectl 命令执行的时候究竟发生了什么(kubectl、apiserver、etcd)

    参考: https://www.yangcs.net/posts/what-happens-when-k8s/ 总而言之,kubectl命令执行的时候,先在本地封装请求,然后过kube-apiserv ...

  10. typescript那些事儿

    本篇文章介绍typescript那些. ts-loader会调用typescript去编译.ts,typescript会读取tsconfig.json配置来决定如何编译. 在tsx文件中使用样式,需要 ...