1 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 39 public class ServletContextAttributeFactoryBean implements FactoryBean, ServletContextAware { 40 41 private String attributeName; 42 43 private Object attribute; 44 45 46 49 public void setAttributeName(String attributeName) { 50 this.attributeName = attributeName; 51 } 52 53 public void setServletContext(ServletContext servletContext) { 54 if (this.attributeName == null) { 55 throw new IllegalArgumentException ("attributeName is required"); 56 } 57 this.attribute = servletContext.getAttribute(this.attributeName); 58 if (this.attribute == null) { 59 throw new IllegalStateException ("No ServletContext attribute '" + this.attributeName + "' found"); 60 } 61 } 62 63 64 public Object getObject() throws Exception { 65 return this.attribute; 66 } 67 68 public Class getObjectType() { 69 return (this.attribute != null ? this.attribute.getClass() : null); 70 } 71 72 public boolean isSingleton() { 73 return true; 74 } 75 76 } 77 | Popular Tags |