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.webapp.controller.action;
17  
18  import org.apache.struts.action.ActionServlet;
19  import org.springframework.context.ApplicationContext;
20  import org.springframework.web.context.support.WebApplicationContextUtils;
21  
22  /***
23   * Loads Spring Application Context and exposes its beans
24   * 
25   * @author Carlos Sanchez
26   * @version $Revision: 1.1 $
27   */
28  public class SpringActionSupport {
29  
30      private ApplicationContext ctx = null;
31  
32      private ActionServlet actionServlet = null;
33  
34      public SpringActionSupport(ActionServlet actionServlet) {
35          this.actionServlet = actionServlet;
36      }
37  
38      /***
39       * Gets a bean from Spring configuration
40       * 
41       * @param name
42       *            bean name
43       * @return the bean
44       */
45      public Object getBean(String name) {
46          if (ctx == null) {
47              ctx = WebApplicationContextUtils
48                      .getRequiredWebApplicationContext(actionServlet
49                              .getServletContext());
50          }
51          return ctx.getBean(name);
52      }
53  
54  }