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);
}
}

最新文章

  1. Oracle学习笔记四 SQL命令(二):SQL操作语言类别
  2. Sybase
  3. XCode6.0的iOS免证书真机测试方法(MAC及黑苹果均有效)
  4. yii2整理
  5. 63. Swap Nodes in Pairs &amp;&amp; Rotate List &amp;&amp; Remove Nth Node From End of List
  6. 【Django】Django 如何使用 Django设置的日志?
  7. POJ 1321棋盘问题
  8. Win7允许被ping
  9. 在Ubuntu Server14.04上编译Android6.0源码
  10. 《AngularJS》--指令的相互调用
  11. .NET基础拾遗(4)委托为何而生?
  12. [Immutable,js] Iterating Over an Immutable.js Map()
  13. linux软件安装(rpm,源码编译)
  14. 小米wifi
  15. 【最短路&#183;差分约束】洛谷P1250
  16. .net core 接口返回图片并且进行压缩
  17. Deseq2 的可视化策略汇总
  18. Druid学习之路 (五)Druid的数据摄取任务类型
  19. 使用游标、存储过程、pivot 三种方法导入数据
  20. The “SignFile” task was not given a value for the required parameter “CertificateThumbprint”的一个简单的解决方法

热门文章

  1. SQL Server 2016 查询存储性能优化小结
  2. go语言字符串的连接和截取
  3. 三层构架和 MVC的区别和理解
  4. List接口的实现类(Vector)(与ArrayList相似,区别是Vector是重量级的组件,使用使消耗的资源比较多。)
  5. idea创建maven SSM项目
  6. MYSQL 文件类型
  7. k8s(6)-滚动更新
  8. RecyclerView+SwpieRefreshLayout(转载)
  9. git bash here右键菜单
  10. Ext Js 6+ 动态切换皮肤