001/* 002 * Copyright 2012-2015 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 * http://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.boot.autoconfigure.web; 018 019import org.springframework.beans.BeansException; 020import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 021import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 022import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 023import org.springframework.boot.autoconfigure.condition.SearchStrategy; 024import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; 025import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; 026import org.springframework.boot.context.properties.EnableConfigurationProperties; 027import org.springframework.context.ApplicationContext; 028import org.springframework.context.ApplicationContextAware; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Configuration; 031import org.springframework.core.Ordered; 032import org.springframework.util.Assert; 033import org.springframework.util.StringUtils; 034 035/** 036 * {@link EnableAutoConfiguration Auto-configuration} that configures the 037 * {@link ConfigurableEmbeddedServletContainer} from a {@link ServerProperties} bean. 038 * 039 * @author Dave Syer 040 * @author Andy Wilkinson 041 */ 042@Configuration 043@EnableConfigurationProperties 044@ConditionalOnWebApplication 045public class ServerPropertiesAutoConfiguration { 046 047 @Bean 048 @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) 049 public ServerProperties serverProperties() { 050 return new ServerProperties(); 051 } 052 053 @Bean 054 public DuplicateServerPropertiesDetector duplicateServerPropertiesDetector() { 055 return new DuplicateServerPropertiesDetector(); 056 } 057 058 /** 059 * {@link EmbeddedServletContainerCustomizer} that ensures there is exactly one 060 * {@link ServerProperties} bean in the application context. 061 */ 062 private static class DuplicateServerPropertiesDetector implements 063 EmbeddedServletContainerCustomizer, Ordered, ApplicationContextAware { 064 065 private ApplicationContext applicationContext; 066 067 @Override 068 public int getOrder() { 069 return 0; 070 } 071 072 @Override 073 public void setApplicationContext(ApplicationContext applicationContext) 074 throws BeansException { 075 this.applicationContext = applicationContext; 076 } 077 078 @Override 079 public void customize(ConfigurableEmbeddedServletContainer container) { 080 // ServerProperties handles customization, this just checks we only have 081 // a single bean 082 String[] serverPropertiesBeans = this.applicationContext 083 .getBeanNamesForType(ServerProperties.class); 084 Assert.state(serverPropertiesBeans.length == 1, 085 "Multiple ServerProperties beans registered " + StringUtils 086 .arrayToCommaDelimitedString(serverPropertiesBeans)); 087 } 088 089 } 090 091}