001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.context.support; 018 019import javax.servlet.ServletConfig; 020import javax.servlet.ServletContext; 021 022import org.springframework.core.env.Environment; 023import org.springframework.core.env.MutablePropertySources; 024import org.springframework.core.env.PropertySource; 025import org.springframework.core.env.PropertySource.StubPropertySource; 026import org.springframework.core.env.StandardEnvironment; 027import org.springframework.jndi.JndiLocatorDelegate; 028import org.springframework.jndi.JndiPropertySource; 029import org.springframework.lang.Nullable; 030import org.springframework.web.context.ConfigurableWebEnvironment; 031 032/** 033 * {@link Environment} implementation to be used by {@code Servlet}-based web 034 * applications. All web-related (servlet-based) {@code ApplicationContext} classes 035 * initialize an instance by default. 036 * 037 * <p>Contributes {@code ServletConfig}, {@code ServletContext}, and JNDI-based 038 * {@link PropertySource} instances. See {@link #customizePropertySources} method 039 * documentation for details. 040 * 041 * @author Chris Beams 042 * @since 3.1 043 * @see StandardEnvironment 044 */ 045public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment { 046 047 /** Servlet context init parameters property source name: {@value}. */ 048 public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams"; 049 050 /** Servlet config init parameters property source name: {@value}. */ 051 public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams"; 052 053 /** JNDI property source name: {@value}. */ 054 public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties"; 055 056 057 /** 058 * Customize the set of property sources with those contributed by superclasses as 059 * well as those appropriate for standard servlet-based environments: 060 * <ul> 061 * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} 062 * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME} 063 * <li>{@value #JNDI_PROPERTY_SOURCE_NAME} 064 * </ul> 065 * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will 066 * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and 067 * properties found in either of the above take precedence over those found in 068 * {@value #JNDI_PROPERTY_SOURCE_NAME}. 069 * <p>Properties in any of the above will take precedence over system properties and 070 * environment variables contributed by the {@link StandardEnvironment} superclass. 071 * <p>The {@code Servlet}-related property sources are added as 072 * {@link StubPropertySource stubs} at this stage, and will be 073 * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized} 074 * once the actual {@link ServletContext} object becomes available. 075 * @see StandardEnvironment#customizePropertySources 076 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources 077 * @see ServletConfigPropertySource 078 * @see ServletContextPropertySource 079 * @see org.springframework.jndi.JndiPropertySource 080 * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources 081 * @see #initPropertySources(ServletContext, ServletConfig) 082 */ 083 @Override 084 protected void customizePropertySources(MutablePropertySources propertySources) { 085 propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); 086 propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); 087 if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { 088 propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); 089 } 090 super.customizePropertySources(propertySources); 091 } 092 093 @Override 094 public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { 095 WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); 096 } 097 098}