Springcontextholder Static hold Springcontext reference

Source: Internet
Author: User

Import Java.util.map;import Org.springframework.context.applicationcontext;import org.springframework.context.applicationcontextaware;/** * * Save spring ApplicationContext with static variables, Applicaitoncontext can be removed from any code anywhere.  * @author Zhuh */public class Springcontextholder implements applicationcontextaware{private static ApplicationContext        ApplicationContext;    Implement the context injection function of the Applicationcontextaware interface and store it in a static variable.  public void Setapplicationcontext (ApplicationContext applicationcontext) {Springcontextholder.applicationcontext =    ApplicationContext;    }//Gets the applicationcontext stored in the static variable.        public static ApplicationContext Getapplicationcontext () {checkapplicationcontext ();    return applicationcontext;    }//Gets the bean from the static variable ApplicationContext and automatically transforms to the type of the assigned value object.        @SuppressWarnings ("unchecked") public static <T> T Getbean (String name) {Checkapplicationcontext ();    Return (T) Applicationcontext.getbean (name); }//from static variable ApplicationcoGets the bean in the ntext and automatically transforms it into the type of the assigned value object.    If more than one bean matches the class, remove the first one. @SuppressWarnings ("unchecked") public static <T> T Getbean (class<t> clazz) {Checkapplicationcontext        ();        @SuppressWarnings ("Rawtypes") Map beanmaps = Applicationcontext.getbeansoftype (clazz);        if (Beanmaps!=null &&!beanmaps.isempty ()) {return (T) beanmaps.values (). iterator (). Next ();        } else{return null; }} private static void Checkapplicationcontext () {if (ApplicationContext = = null) {throw new Il        Legalstateexception ("Applicaitoncontext not injected, please define Springcontextholder in Applicationcontext.xml"); }    }}

<!--for holding ApplicationContext, you can use the static method of Springcontextholder.getbean (' xxxx ') to get the Spring bean object--

<beans Class="Com.xxxxx.SpringContextHolder" />

The tool class is used primarily for: classes that are not managed by the spring framework but are called by the Bean in the spring container.

In spring, the corresponding object is obtained through IOC dependency injection, but the class saves the spring applicationcontext with static variables by implementing the Applicationcontextaware interface. Applicaitoncontext can be removed from any code anywhere.

So you can't talk about Org.springframework.context.ApplicationContextAware this interface:

Once a class implements this interface (Applicationcontextaware), this class can easily get all the beans in the ApplicationContext. In other words, this class can get directly from the spring configuration file, all the bean objects that are referenced to.






Springcontextholder Static hold Springcontext reference

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.