1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.testing;
17
18 import org.springframework.context.ApplicationContext;
19 import org.springframework.context.support.ClassPathXmlApplicationContext;
20
21 /***
22 * Utility class to cache ApplicationContext loaded from xml classpath files. It
23 * is autoconfigured in the xml file called "applicationContext.xml" loaded from
24 * the classpath with a bean definition that calls this class constructor with a
25 * list of file names.
26 *
27 * <pre>
28 * <bean id="anyName"
29 * class="net.sf.oness.common.model.testing.SpringApplicationContext">
30 * <constructor-arg>
31 * <list>
32 * <value>applicationContext-oness-common-model.xml</value>
33 * <value>applicationContext-test.xml</value>
34 * </list>
35 * </constructor-arg>
36 * </bean>
37 * </pre>
38 *
39 * @author Carlos Sanchez
40 * @version $Revision: 1.1 $
41 */
42 public class SpringApplicationContext {
43
44 private static final String DEFAULT_FILE_NAME = "applicationContext.xml";
45
46 private static ApplicationContext applicationContext;
47
48 private static String[] fileNames;
49
50 static {
51 new ClassPathXmlApplicationContext(DEFAULT_FILE_NAME);
52 applicationContext = new ClassPathXmlApplicationContext(fileNames);
53 }
54
55 public SpringApplicationContext(String[] fileNames) {
56 SpringApplicationContext.fileNames = fileNames;
57 }
58
59 /***
60 * Get the spring application context
61 *
62 * @return
63 */
64 public static ApplicationContext getApplicationContext() {
65 return applicationContext;
66 }
67
68 /***
69 * Get a bean from the application context
70 *
71 * @param name
72 * @return
73 */
74 public static Object getBean(String name) {
75 return getApplicationContext().getBean(name);
76 }
77
78 }