1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.sys.context;
17
18 import org.kuali.kpme.core.sys.constants.SpringBeans;
19 import org.springframework.beans.BeansException;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.ApplicationContextAware;
22
23
24
25
26
27
28 public class SpringContext implements ApplicationContextAware {
29
30 private static ApplicationContext applicationContext;
31
32
33
34
35
36
37
38 public SpringContext() throws Exception {
39 if (applicationContext != null)
40 throw new Exception(
41 "Cannot instantiate SpringContext twice. Use the static methods");
42 }
43
44
45
46
47
48
49
50
51 @Override
52 public void setApplicationContext(ApplicationContext applicationContext)
53 throws BeansException {
54 SpringContext.applicationContext = applicationContext;
55 }
56
57
58
59
60
61
62
63 public static Object getBean(SpringBeans bean) {
64 return applicationContext.getBean(bean.toString());
65 }
66
67
68
69
70
71
72
73
74 @SuppressWarnings("unchecked")
75 public static <T> T getBean(Class<T> clazz) {
76 String[] beanNames = applicationContext.getBeanNamesForType(clazz);
77
78 return (T) applicationContext.getBean(beanNames[0]);
79 }
80 }