Save Spring applicationcontext with static variables

Source: Internet
Author: User

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=&lt=&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.