01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.web.context.support;
18:
19: import javax.servlet.ServletContext;
20:
21: import org.springframework.beans.factory.FactoryBean;
22: import org.springframework.web.context.ServletContextAware;
23:
24: /**
25: * Simple FactoryBean that exposes the ServletContext for bean references.
26: * Can be used as alternative to implementing the ServletContextAware
27: * callback interface. Allows for passing the ServletContext reference
28: * to a constructor argument or any custom bean property.
29: *
30: * <p>Note that there's a special FactoryBean for exposing a specific
31: * ServletContext attribute, named ServletContextAttributeFactoryBean.
32: * So if all you need from the ServletContext is access to a specific
33: * attribute, ServletContextAttributeFactoryBean allows you to expose
34: * a constructor argument or bean property of the attribute type,
35: * which is a preferable to a dependency on the full ServletContext.
36: *
37: * @author Juergen Hoeller
38: * @since 1.1.4
39: * @see javax.servlet.ServletContext
40: * @see org.springframework.web.context.ServletContextAware
41: * @see ServletContextAttributeFactoryBean
42: */
43: public class ServletContextFactoryBean implements FactoryBean,
44: ServletContextAware {
45:
46: private ServletContext servletContext;
47:
48: public void setServletContext(ServletContext servletContext) {
49: this .servletContext = servletContext;
50: }
51:
52: public Object getObject() {
53: return this .servletContext;
54: }
55:
56: public Class getObjectType() {
57: return (this .servletContext != null ? this .servletContext
58: .getClass() : ServletContext.class);
59: }
60:
61: public boolean isSingleton() {
62: return true;
63: }
64:
65: }
|