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