01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.spring;
16:
17: import javax.servlet.ServletContext;
18:
19: import org.springframework.web.context.WebApplicationContext;
20: import org.springframework.web.context.support.WebApplicationContextUtils;
21: import org.strecks.exceptions.ApplicationConfigurationException;
22:
23: /**
24: * Spring-related utility methods
25: * @author Phil Zoio
26: */
27: public class SpringUtils {
28:
29: /**
30: * Returns named Spring bean from <code>ServletContext</code>. Throws
31: * <code>ApplicationConfigurationException</code> if Spring context is not present or named bean is not present in context
32: */
33: public static Object getSpringBean(ServletContext servletContext,
34: String beanName) {
35: WebApplicationContext context = WebApplicationContextUtils
36: .getWebApplicationContext(servletContext);
37:
38: if (context == null) {
39: throw new ApplicationConfigurationException(
40: "No spring root context found using "
41: + "WebApplicationContextUtils.getWebApplicationContext(servletContext). This is probably an application configuration error");
42: }
43:
44: Object bean = context.getBean(beanName);
45: if (bean == null) {
46: throw new ApplicationConfigurationException(
47: "No spring bean "
48: + beanName
49: + " found. This is probably an application configuration error");
50: }
51:
52: return bean;
53: }
54:
55: }
|