加载spring上下文 工具类
来源:互联网 发布:专业 知乎 编辑:程序博客网 时间:2024/06/01 11:32
工具类代码如下
package com.hzy.oss.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtils implements ApplicationContextAware{
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
if(SpringUtils.applicationContext == null){
SpringUtils.applicationContext = applicationContext;
}
}
//获取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);
}
}
使用代码
SearchItemService searchItemService=(SearchItemService)SpringUtils.getBean("searchItemServiceImp");
阅读全文
0 0
- 加载spring上下文 工具类
- spring 如何加载上下文
- 加载Spring上下文环境ApplicationContextAware
- ApplicationContextAware加载Spring上下文环境
- 获得spring上下文工具SpringContextUtil
- 真正理解线程上下文类加载器:tomcat and Spring
- 上下文类加载器
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 加载spring上下文几种方式汇总
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 不重启服务器重新加载Spring上下文
- 通过ApplicationContextAware加载Spring上下文环境
- Spring 上下文工厂类
- spring上下文辅助类
- 线程上下文类加载器
- mybaties 一对多 list<String> 返回数据
- Qt之进程间通信(QProcess)
- jsp trimDirectiveWhitespaces ="true"
- AS调试opencv示例代码,尝试解决camera2包报错的问题
- Java基础(十二)----concurrentHashMap与线程池
- 加载spring上下文 工具类
- 可随意移动子VIEW的布局文件
- 简单创建spring boot 项目。
- Unity5之Network基础用法之人物跑动同步
- Java的native方法
- LTE物理传输资源(3)-时频资源
- 文件下载利用poi导出excel
- 宿主机为win10,虚拟机为 win7系统下载及安装virtio-drivers网卡驱动, 以提高网
- matlabR2012a 永久激活文件 lic_standalone.dat 文件