【ApplicationContext】通过实现ApplicationContextAware接口获取bean
SpringApplicationUtils.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class SpringApplicationUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringApplicationUtils.applicationContext == null) {
SpringApplicationUtils.applicationContext = applicationContext;
}
}
/**
* 通过name获取 Bean.
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
/**
* 获取指定类型的所有bean实例
*
* @param clazz
* @param <T>
* @return
*/
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
Map<String, T> instances = getApplicationContext().getBeansOfType(clazz);
return instances;
}
}
【ApplicationContext】通过实现ApplicationContextAware接口获取bean的更多相关文章
- ApplicationContextAware 快速获取bean
在Web应用中,Spring容器通常采用声明式方式配置产生:开发者只要在web.xml中配置一个Listener,该Listener将会负责初始化Spring容器,MVC框架可以直接调用Spring容 ...
- 多线程中实现ApplicationContextAware接口获取需要的bean,applicationContext.getBea未返回也未报错
唉,面试失败了有点难过. https://q.cnblogs.com/q/95168/#a_208239
- ApplicationContextAware获取bean
ApplicationContextAware获取bean 概述 在某些特殊的情况下,Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该Bean先获取Spring容器 ...
- ApplicationContextAware 接口
一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...
- ApplicationContextAware 接口的作用
接口说明:当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象 ...
- ApplicationContextAware接口的作用
1/============== 在Web应用中,Spring容器通常采用声明式方式配置产生:开发者只要在web.xml中配置一个Listener,该Listener将会负责初始化Spring容器 ...
- spring中ApplicationContextAware接口使用理解
一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...
- Spring获取bean工具类,可用于在线程里面获取bean
Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...
- springmvc手动获取bean
@Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, Disposabl ...
随机推荐
- scrapy常用命令
终端命令 创建一个项目: scrapy startproject name 利用蜘蛛名创建一个py文件: scrapy genspider name domain.name 在终端运行:scrapy ...
- Unity 3D接入ShareSDK流程2
Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- kmp--看毛片算法
string str; int next[N];// 核♥: next[k] 字符串前(k-1)个元素有next[k]个相等前后缀 // 初始化 next[0]=-1; next[1]=0; void ...
- HPU组队赛J:Ball King(线段树)
时间限制 1 Second 内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...
- HDU 1159:Common Subsequence(LCS模板)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- nginx的安装 、Nginx默认虚拟主机、nginx用户认证、nginx 域名重定向
1.nginx:官网:www.nginx.org 下载:wget -c http://nginx.org/download/nginx-1.14.0.tar.gz解压:tar -zxvf nginx ...
- tomcat部署成https协议
1 生成密匙:进入jdk的bin目录后输入: keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keysto ...
- python os模块使用笔记(更新)
import os 添加os模块 walk方法: os.walk(path) path是string形式的目标目录 生成一个某目录下递归树形目录迭代器,方便递归访问子目录,访问目录就能够轻松访问子文件 ...
- 服务器cpu负载过高问题排查
https://blog.csdn.net/MrZhangXL/article/details/77711996 第一步 :执行top命令,查出当前机器线程情况 top - 09:14:36 up 1 ...
- [转]一致性hash算法 - consistent hashing
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...