01: package org.contineo.util;
02:
03: import org.springframework.beans.BeansException;
04: import org.springframework.context.ApplicationContext;
05: import org.springframework.context.ApplicationContextAware;
06: import org.springframework.context.support.AbstractApplicationContext;
07:
08: /**
09: * Utility class collecting static methods related to spring's context.
10: *
11: * @author Marco Meschieri
12: * @version $Id:$
13: * @since 3.0
14: */
15: public class Context implements ApplicationContextAware {
16:
17: // Singleton instance
18: private static Context instance;
19:
20: // The Spring's application context
21: private static ApplicationContext applicationContext;
22:
23: private Context() {
24: Context.instance = this ;
25: }
26:
27: public static Context getInstance() {
28: return instance;
29: }
30:
31: public void setApplicationContext(
32: ApplicationContext applicationContext)
33: throws BeansException {
34: Context.applicationContext = applicationContext;
35: }
36:
37: /**
38: * Retrieves a bean registered in the Spring context.
39: *
40: * @param id The bean identifier
41: * @return The bean instance
42: */
43: public Object getBean(String id) {
44: return applicationContext.getBean(id);
45: }
46:
47: /**
48: * Retrieves a bean registered in the Spring context using a class name. At
49: * first the fully qualified class name is checked, then if nothing was
50: * found the simple class name is used as bean id.
51: *
52: * @param clazz The bean identifier as class name
53: * @return The bean instance
54: */
55: public Object getBean(Class clazz) {
56: String id = clazz.getName();
57:
58: if (!applicationContext.containsBean(id))
59: id = id.substring(id.lastIndexOf('.') + 1);
60:
61: return getBean(id);
62: }
63:
64: /**
65: * Reloads the Spring application context.
66: * <p>
67: * NOTE: use carefully, invoke only during setup when the config.xml is
68: * changed
69: */
70: public static void refresh() {
71: if (applicationContext != null)
72: ((AbstractApplicationContext) applicationContext).refresh();
73: }
74: }
|