package cn.com.util; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; public SpringUtil() { } public static void setApplicationContexts(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static <T> T getBean(String name) throws BeansException { return applicationContext.getBean(name); } public static Map<String, ?> getBeansOfType(Class<?> class1) { return applicationContext.getBeansOfType(class1); } public static <T> T getBean(Class<T> type) throws BeansException { return applicationContext.getBean(type); } public static <T> T getBean(String beanName, Class<T> type) throws BeansException { return applicationContext.getBean(beanName, type); } public static String getToken(HttpServletRequest request) { Cookie[] cookie = request.getCookies(); if (cookie == null) { return null; } else { for(int i = 0; i < cookie.length; ++i) { Cookie cook = cookie[i]; if (cook.getName().equals("SESSION") && !"".equals(cook.getValue() + "".trim())) { return cook.getValue().toString(); } } return null; } } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } }