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

AppUtil的更多相关文章

  1. Android AppUtil通用类

    1.整体分析 1.1.先看一下源码,可以直接Copy. public class AppUtil { /** * 获取当前程序包名 * * @param context 上下文 * @return 程 ...

  2. org.springframework.context.ApplicationContextAware使用理解

    一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...

  3. ApplicationContextAware 接口

    一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...

  4. [转]Maven 划分模块

    所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...

  5. 从零开始搭建架构实施Android项目

    我们先假设一个场景需求:刚有孩子的爸爸妈妈对用照片.视频记录宝宝成长有强烈的意愿,但苦于目前没有一款专门的手机APP做这件事.A公司洞察到市场需求,要求开发团队尽快完成Android客户端的开发.以下 ...

  6. nodeJS+express+Jade写一个局域网聊天应用(node基础)

    为了复习一下nodeJS, 而且socketIO这东西听起来就好高端有木有, 而且有人写过了open, 也可以作为自己的参考有木有, 点击下载源代码: express是4.x的版本, 跟以前的配置有些 ...

  7. Maven管理 划分模块

    转载地址:juvenshun.iteye.com/blog/305865 “分天下为三十六郡,郡置守,尉,监” —— <史记·秦始皇本纪> 所有用Maven管理的真实的项目都应该是分模块的 ...

  8. jquery模板制作左侧导航组件

    /** * Created by bmk on 16-4-25. * * 用法:在自己的js里面把左侧导航的相关图标和对应的列表项名称如下编写: * 版本更新日至按需添加 * 在js中的RNA.run ...

  9. Spring中Aware相关接口原理

    Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...

随机推荐

  1. centos7配置固定ip

    查看本机gateway netstat -rn (以0.0.0.0开始的行的gateway是默认网关) vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 T ...

  2. oracle 优化or 更换in、exists、union all几个字眼

    oracle 优化or 更换in.exists.union几个字眼.测试没有问题! 根据实际情况选择相应的语句是.假设指数,or全表扫描,in 和not in 应慎用.否则会导致全表扫描.  sele ...

  3. 第四百一十四节,python常用算法学习

    本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机 ...

  4. PowerDesigner 中模型设计导出Excel表格

    今天项目做设计,客户要看数据设计,需要到处Excel表格.去网上搜索下,把使用总结如下: 已经完成设计的pd设计 打开pd,快捷键Ctrl + Shift + X或者Tools>Exectue ...

  5. [redis] mac下redis安装、设置、启动停止

    From: https://www.cnblogs.com/shoren/p/redis.html 下载安装 需要下载release版本,下载地址: http://download.redis.io/ ...

  6. Google API Design Guide (谷歌API设计指南)中文版

    面向资源的设计 这份设计指南的目标是帮助开发人员设计简单.一致.易用的网络API.同时,它也有助于收敛基于socket的API和(注:原文是with,这里翻译为“和”)基于HTTP的REST API. ...

  7. js中文输入法字符串截断

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. swoole 基础知识

    $host = 127.0.0.1  监听本地ip $host = 255.3.258.139  监听外网的某个ip $host= 0.0.0.0 监听所有ip $ws = new swoole_we ...

  9. sql知识点记录

    order by就是排序. group by就是分组. WHERE语句在GROUP BY语句之前:SQL会在分组之前计算WHERE语句.    HAVING语句在GROUP BY语句之后:SQL会在分 ...

  10. oracle客户端instantclient如何配置

    下载下来的instantclient-basic-nt-11.2.0.4.0.zip文件解压缩D:\Program Files\instantclient_11_2(可以选择自己的目录) (1) 增加 ...