Spring Service locator, which can fetch beans from anywhere

Source: Internet
Author: User

By holding the spring application scenario ApplicationContext, you can get the beans from anywhere.

1 ImportOrg.apache.commons.logging.Log;2 Importorg.apache.commons.logging.LogFactory;3 ImportOrg.springframework.beans.factory.DisposableBean;4 ImportOrg.springframework.context.ApplicationContext;5 ImportOrg.springframework.context.ApplicationContextAware;6 7 /**8 * Service Locator9 * Hold the Spring application scenario and get the beans from anywhere.Ten  */ One  Public Final classServicelocatorImplementsApplicationcontextaware, Disposablebean { A      -     Private StaticLog logger = Logfactory.getlog (servicelocator.class); -     Private StaticApplicationContext context =NULL; the      -     /** - * Implement the Applicationcontextaware interface and inject the context into the static variable. -      * @paramContext +      */ - @Override +      Public voidSetapplicationcontext (ApplicationContext context) { ALogger.debug ("injected the ApplicationContext into Servicelocator:" +context); at         if(Servicelocator.context! =NULL) { -Logger.debug ("[------------ApplicationContext in the Servicelocator" + -"is covered, as the original ApplicationContext is:" -+ Servicelocator.context + "------------]"); -         } -Servicelocator.context =context; in     } -      to     /** + * Implement the Disposablebean interface to clean up static variables when the context is closed. -      */ the @Override *      Public voidDestroy ()throwsException { $ servicelocator.clear ();Panax Notoginseng     } -      the     /** + * Gets the applicationcontext stored in the static variable. A      * @return the      */ +      Public StaticApplicationContext Getapplicationcontext () { - assertcontextinjected (); $         returncontext; $     } -      -     /** the * Get the bean from the spring scenario and automatically transform it to the type of the assigned value object. -      * @paramName Bean namesWuyi      * @returnBean Object the      */ -@SuppressWarnings ("Unchecked") Wu      Public Static<T>T GetService (String name) { - assertcontextinjected (); About         return(T) Context.getbean (name); $     } -      -     /** - * Get the bean from the spring scenario and automatically transform it to the type of the assigned value object. A      * @paramRequiredtype Bean Class +      * @returnBean Object the      */ -      Public Static<T> T GetService (class<t>requiredtype) { $ assertcontextinjected (); the         returnContext.getbean (requiredtype); the     } the      the     /** - * Clear the ApplicationContext in Servicelocator in      */ the      Public Static voidClear () { theLogger.debug ("Clear ApplicationContext in Servicelocator:" +context); AboutContext =NULL; the     } the      the     /** + * Check that the ApplicationContext is not empty. -      */ the     Private Static voidassertcontextinjected () {Bayi         if(Context = =NULL) { the             Throw NewIllegalStateException ("Applicaitoncontext not injected," + the"As defined in the Context.xml servicelocator"); -         } -     } the}
View Code

Call the GetService function, which is obtained by name and type.

1     /**2 * Get the bean from the spring scenario and automatically transform it to the type of the assigned value object.3      * @paramName Bean names4      * @returnBean Object5      */6@SuppressWarnings ("Unchecked")7      Public Static<T>T GetService (String name) {8 assertcontextinjected ();9         return(T) Context.getbean (name);Ten     } One      A     /** - * Get the bean from the spring scenario and automatically transform it to the type of the assigned value object. -      * @paramRequiredtype Bean Class the      * @returnBean Object -      */ -      Public Static<T> T GetService (class<t>requiredtype) { - assertcontextinjected (); +         returnContext.getbean (requiredtype); -}

Spring Service locator, which can fetch beans from anywhere

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.