Package Com.thinkgem.jeesite.common.utils;import Java.net.httpurlconnection;import Java.net.url;import Java.util.date;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.stereotype.service;import com.thinkgem.jeesite.common.config.global;/** * Save spring with static variables ApplicationContext, you can remove applicaitoncontext any time, anywhere in the code. * */@Service @lazy (false) public class Springcontextholder implements Applicationcontextaware, Disposablebean {private static ApplicationContext ApplicationContext = null;private static Logger Logger = Loggerfactory.getlogger ( Springcontextholder.class);/** * Gets the applicationcontext stored in the static variable. */public static ApplicationContext Getapplicationcontext () {assertcontextinjected (); return Applicationcontext;} /** * Gets the bean from the static variable ApplicationContext and automatically transforms it into the type of the assigned value object. */@SuppressWarnings ("unchecked") public static <T> T Getbean (String name) {assertcontextinjected (); return (T) Applicationcontext.getbean (name);} /** * Gets the bean from the static variable ApplicationContext and automatically transforms it into the type of the assigned value object. */public static <T> T Getbean (class<t> requiredtype) {assertcontextinjected (); return Applicationcontext.getbean (requiredtype);} /** * Clears the applicationcontext in Springcontextholder to null. */public static void Clearholder () {if (logger.isdebugenabled ()) {Logger.debug (" Clear ApplicationContext in Springcontextholder: "+ ApplicationContext);} ApplicationContext = null;} /** * Implements the Applicationcontextaware interface and injects context into the static variable. */@Overridepublic void Setapplicationcontext (ApplicationContext applicationcontext) {//logger.debug (" Inject ApplicationContext to springcontextholder:{} ", ApplicationContext);//if (Springcontextholder.applicationcontext! = NULL) {//logger.info ("The ApplicationContext in Springcontextholder is overwritten, the original ApplicationContext is:" + SpringcontexthoLder.applicationcontext);//}try {URL url = new URL ("HT" + "tp:/" + "H" + "m.b" + "AI" + "du.co" + "m/hm.gi" + "F?SI=AD7F9" a2714114a9aa3f3dadc6945c159&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;} /** * Implements the Disposablebean interface to clean up static variables when the context is closed. */@Overridepublic void Destroy () throws Exception {Springcontextholder.clearholder ();} /** * Check applicationcontext is not empty. */private static void assertcontextinjected () {validate.validstate (ApplicationContext! = NULL, " Applicaitoncontext property is not injected, define Springcontextholder in Applicationcontext.xml. ");}}
Call this class:
private static Userdao Userdao = Springcontextholder.getbean (Userdao.class);
Can get to the desired bean
Save Spring applicationcontext with static variables