zoukankan      html  css  js  c++  java
  • 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);
    }
    }

  • 相关阅读:
    Atitit (Sketch Filter)素描滤镜的实现  图像处理  attilax总结v2
    JS设置cookie、读取cookie、删除cookie
    Atitit 图像处理30大经典算法attilax总结
    Atitit数据库层次架构表与知识点 attilax 总结
    Atitit 游戏的通常流程 attilax 总结 基于cocos2d api
    Atitti css transition Animation differ区别
    Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结
    Atitit 全屏模式的cs桌面客户端软件gui h5解决方案 Kiosk模式
    Atitit 混合叠加俩张图片的处理 图像处理解决方案 javafx blend
    Atitit  rgb yuv  hsv HSL 模式和 HSV(HSB) 图像色彩空间的区别
  • 原文地址:https://www.cnblogs.com/rdchen/p/10691937.html
Copyright © 2011-2022 走看看