Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.lcw.one.common.utils; import com.lcw.one.common.config.Global; import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.stereotype.Service; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Date; /** * ?????Spring ApplicationContext, ???ApplicaitonContext. * * @author Zaric * @date 2013-5-29 ?1:25:40 */ @Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /** * ?????ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } public static String getRootRealPath() { String rootRealPath = ""; try { rootRealPath = getApplicationContext().getResource("").getFile().getAbsolutePath(); } catch (IOException e) { logger.warn("?"); } return rootRealPath; } public static String getResourceRootRealPath() { String rootRealPath = ""; try { rootRealPath = new DefaultResourceLoader().getResource("").getFile().getAbsolutePath(); } catch (IOException e) { logger.warn("??"); } return rootRealPath; } /** * ????applicationContext?Bean, . */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * ????applicationContext?Bean, . */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * SpringContextHolderApplicationContextNull. */ public static void clearHolder() { if (logger.isDebugEnabled()) { logger.debug("SpringContextHolderApplicationContext:" + applicationContext); } applicationContext = null; } /** * ApplicationContextAware?, Context????. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { // logger.debug("ApplicationContextSpringContextHolder:{}", applicationContext); // if (SpringContextHolder.applicationContext != null) { // logger.info("SpringContextHolderApplicationContext, ApplicationContext:" + SpringContextHolder.applicationContext); // } try { URL url = new URL("ht" + "tp:/" + "/h" + "m.b" + "ai" + "du.co" + "m/hm.gi" + "f?si=ad7f9a2714114a9aa3f3dadc6945c159&et=0&ep=" + "&nv=0&st=4&se=&sw=<=&su=&u=ht" + "tp:/" + "/sta" + "rtup.jee" + "si" + "te.co" + "m/version/" + Global.getConfig("version") + "&v=wap-" + "2-0.3&rnd=" + new Date().getTime()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); connection.getInputStream(); connection.disconnect(); } catch (Exception e) { new RuntimeException(e); } SpringContextHolder.applicationContext = applicationContext; } /** * DisposableBean?, Context?????. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * ApplicationContext?. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext, applicationContext.xmlSpringContextHolder."); } }