View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * &lt;bean id=&quot;anyName&quot; 
29   *     class=&quot;net.sf.oness.common.model.testing.SpringApplicationContext&quot;&gt;
30   *     &lt;constructor-arg&gt;
31   *         &lt;list&gt;
32   *             &lt;value&gt;applicationContext-oness-common-model.xml&lt;/value&gt;
33   *             &lt;value&gt;applicationContext-test.xml&lt;/value&gt;
34   *         &lt;/list&gt;
35   *     &lt;/constructor-arg&gt;
36   * &lt;/bean&gt;
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  }