Spring service locator, which can be used to obtain beans and springbeans from any place
By using the Spring Application Scenario ApplicationContext, bean can be obtained anywhere.
1 import org. apache. commons. logging. log; 2 import org. apache. commons. logging. logFactory; 3 import org. springframework. beans. factory. disposableBean; 4 import org. springframework. context. applicationContext; 5 import org. springframework. context. applicationContextAware; 6 7/** 8 * service locator 9 * for Spring applications, you can obtain bean.10 */11 public final class ServiceLocator implements ApplicationContextAware, Dispos AbleBean {12 13 private static Log logger = LogFactory. getLog (ServiceLocator. class); 14 private static ApplicationContext context = null; 15 16/*** 17 * implements the ApplicationContextAware interface and injects the Context into the static variable. 18 * @ param context19 */20 @ Override21 public void setApplicationContext (ApplicationContext context) {22 logger. debug ("Injected the ApplicationContext into ServiceLocator:" + context); 23 if (ServiceLocato R. context! = Null) {24 logger. debug ("[------------ ApplicationContext in the ServiceLocator" + 25 "is covered, as the original ApplicationContext is:" 26 + ServiceLocator. context + "------------]"); 27} 28 ServiceLocator. context = context; 29} 30 31/** 32 * implements the DisposableBean interface to clear static variables when Context is disabled. 33 */34 @ Override35 public void destroy () throws Exception {36 ServiceLocator. clear (); 37} 38 39/** 40 * Get ApplicationContext.41 * @ return42 */43 public static ApplicationContext getApplicationContext () {44 assertContextInjected (); 45 return context; 46} 47 48/** 49 * retrieve beans from Spring application scenarios and automatically convert them to the type of the assigned object. 50 * @ param name bean name 51 * @ return bean object 52 */53 @ SuppressWarnings ("unchecked") 54 public static <T> T getService (String name) {55 assertContextInjected (); 56 return (T) context. getBean (name); 57} 58 59/** 60 * retrieves beans from Spring application scenarios and automatically converts them to the type of the assigned object. 61 * @ param requiredType bean Class 62 * @ return bean object 63 */64 public static <T> T getService (Class <T> requiredType) {65 assertContextInjected (); 66 return context. getBean (requiredType); 67} 68 69/** 70 * clear ApplicationContext71 */72 public static void clear () {73 logger. debug ("Clear ApplicationContext in ServiceLocator:" + context); 74 context = null; 75} 76 77/** 78 * Check that ApplicationContext is not empty. 79 */80 private static void assertContextInjected () {81 if (context = null) {82 throw new IllegalStateException ("ApplicaitonContext not injected," + 83 "as defined in the context. xml ServiceLocator "); 84} 85} 86}View Code
Call the getService function, which is obtained by name and type.
1/*** get Bean from Spring application scenarios and automatically convert it to the type of the assigned object. 3 * @ param name bean name 4 * @ return bean object 5 */6 @ SuppressWarnings ("unchecked") 7 public static <T> T getService (String name) {8 assertContextInjected (); 9 return (T) context. getBean (name); 10} 11 12/** 13 * retrieves beans from Spring application scenarios and automatically converts them to the type of the assigned object. 14 * @ param requiredType bean Class 15 * @ return bean object 16 */17 public static <T> T getService (Class <T> requiredType) {18 assertContextInjected (); 19 return context. getBean (requiredType); 20}