alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (ServletContextAttributeExporter.java)

This example Spring Framework source code file (ServletContextAttributeExporter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

exported, iterator, log, map, overwriting, servlet, servletcontext, servletcontext, servletcontextattributeexporter, servletcontextattributeexporter, servletcontextaware, servletcontextaware, string, string, util

The Spring Framework ServletContextAttributeExporter.java source code

/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.context.support;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.web.context.ServletContextAware;

/**
 * Exporter that takes Spring-defined objects and exposes them as
 * ServletContext attributes. Usually, bean references will be used
 * to export Spring-defined beans as ServletContext attributes.
 *
 * <p>Useful to make Spring-defined beans available to code that is
 * not aware of Spring at all, but rather just of the Servlet API.
 * Client code can then use plain ServletContext attribute lookups
 * to access those objects, despite them being defined in a Spring
 * application context.
 *
 * <p>Alternatively, consider using the WebApplicationContextUtils
 * class to access Spring-defined beans via the WebApplicationContext
 * interface. This makes client code aware of Spring API, of course.
 *
 * @author Juergen Hoeller
 * @since 1.1.4
 * @see javax.servlet.ServletContext#getAttribute
 * @see WebApplicationContextUtils#getWebApplicationContext
 */
public class ServletContextAttributeExporter implements ServletContextAware {

	protected final Log logger = LogFactory.getLog(getClass());

	private Map attributes;

	/**
	 * Set the ServletContext attributes to expose as key-value pairs.
	 * Each key will be considered a ServletContext attributes key,
	 * and each value will be used as corresponding attribute value.
	 * <p>Usually, you will use bean references for the values,
	 * to export Spring-defined beans as ServletContext attributes.
	 * Of course, it is also possible to define plain values to export.
	 * @param attributes Map with String keys and Object values
	 */
	public void setAttributes(Map attributes) {
		this.attributes = attributes;
	}

	public void setServletContext(ServletContext servletContext) {
		for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			String attributeName = (String) entry.getKey();
			if (logger.isWarnEnabled()) {
				if (servletContext.getAttribute(attributeName) != null) {
					logger.warn("Overwriting existing ServletContext attribute with name '" + attributeName + "'");
				}
			}
			servletContext.setAttribute(attributeName, entry.getValue());
			if (logger.isInfoEnabled()) {
				logger.info("Exported ServletContext attribute with name '" + attributeName + "'");
			}
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework ServletContextAttributeExporter.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.