package com.qgz.common.util;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;/**
* Spring 工具类
*
* @author gaojun.zhou
* @date 2016年8月23日
*/
public class SpringUtil implements ServletContextListener {

private static WebApplicationContext springContext; public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
} public void contextDestroyed(ServletContextEvent event) {

} /**
* Gets application context.
*
* @return the application context
*/
public static ApplicationContext getApplicationContext() {
return springContext;
} /**
* Instantiates a new Spring util.
*/
public SpringUtil() {
} /**
* Get bean t.
*
* @param <T> the type parameter
* @param requiredType the required type
* @return the t
*/
public static <T> T getBean(Class<T> requiredType){

if(springContext == null){

throw new RuntimeException("springContext is null.");
}
return springContext.getBean(requiredType);
}

}