AppUtil
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
/**
* 获取ApplicationContext, 调用方法 SpringContextUtil.getContext();<br>
* 在spring文件中配置方法:<br>
* <bean id="springContextUtil" class="com.core.util.AppUtil" />
*
*/
public class AppUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 在线用户
*/
private static Map<Long,OnlineUser> onlineUsers=new LinkedHashMap<Long, OnlineUser>();
private static ServletContext servletContext;
/**
*
* @param servletContext
*/
public static void init(ServletContext _servletContext)
{
servletContext=_servletContext;
}
/**
* 获取web应用的ServletContext对象。
* @return
* @throws Exception
*/
public static ServletContext getServletContext() throws Exception{
return servletContext;
}
/**
* spring启动时注入context
*/
public void setApplicationContext(ApplicationContext contex) throws BeansException {
applicationContext=contex;
}
/**
* 获取spring的上下文。
* @return
*/
public static ApplicationContext getContext(){
return applicationContext;
}
public static List<Class> getImplClass(Class clazz) throws ClassNotFoundException{
List<Class> list=new ArrayList<Class>();
Map map= applicationContext.getBeansOfType(clazz);
for(Object obj : map.values()){
String name=obj.getClass().getName();
int pos=name.indexOf("$$");
if(pos>0){
name=name.substring(0,name.indexOf("$$")) ;
}
Class cls= Class.forName(name);
list.add(cls);
}
return list;
}
/**
* 根据类从spring上下文获取bean。
* @param cls
* @return
*/
public static <C> C getBean(Class<C> cls){
return applicationContext.getBean(cls);
}
/**
* 根据类名从spring上下文获取bean。
* @param cls
* @return
*/
public static Object getBean(String beanId){
return applicationContext.getBean(beanId);
}
/**
* 返回spring所有的bean
* @return
*/
public static String[] getAllBean(){
String allBean[] = applicationContext.getBeanDefinitionNames();
return allBean;
}
/**
* 取得应用程序的绝对路径
* @return
*/
public static String getAppAbsolutePath(){
return servletContext.getRealPath("/");
}
/**
* 在web环境中根据web页面的路径获取对应页面的绝对路径。
* @param path
* @return
*/
public static String getRealPath(String path){
return servletContext.getRealPath(path);
}
public static Map<Long, OnlineUser> getOnlineUsers() {
return onlineUsers;
}
/**
* 获取Classpath物理路径
* @return
*/
public static String getClasspath(){
String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1);
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(1);
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}
/**
* Spring发布事件。
* @param applicationEvent
*/
public static void publishEvent(ApplicationEvent applicationEvent){
applicationContext.publishEvent(applicationEvent);
}
/**
* 获取接口的实现类实例。
*
* @param clazz
* @return
* @throws ClassNotFoundException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, Object> getImplInstance(Class clazz)
throws ClassNotFoundException {
return applicationContext.getBeansOfType(clazz);
}
}
最新文章
- Oracle学习笔记四 SQL命令(二):SQL操作语言类别
- Sybase
- XCode6.0的iOS免证书真机测试方法(MAC及黑苹果均有效)
- yii2整理
- 63. Swap Nodes in Pairs &;&; Rotate List &;&; Remove Nth Node From End of List
- 【Django】Django 如何使用 Django设置的日志?
- POJ 1321棋盘问题
- Win7允许被ping
- 在Ubuntu Server14.04上编译Android6.0源码
- 《AngularJS》--指令的相互调用
- .NET基础拾遗(4)委托为何而生?
- [Immutable,js] Iterating Over an Immutable.js Map()
- linux软件安装(rpm,源码编译)
- 小米wifi
- 【最短路&#183;差分约束】洛谷P1250
- .net core 接口返回图片并且进行压缩
- Deseq2 的可视化策略汇总
- Druid学习之路 (五)Druid的数据摄取任务类型
- 使用游标、存储过程、pivot 三种方法导入数据
- The “SignFile” task was not given a value for the required parameter “CertificateThumbprint”的一个简单的解决方法