001/* 002 * Copyright 2002-2016 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.servlet.support; 018 019import org.springframework.util.ObjectUtils; 020import org.springframework.web.context.WebApplicationContext; 021import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 022 023/** 024 * Base class for {@link org.springframework.web.WebApplicationInitializer} 025 * implementations that register a 026 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} 027 * configured with annotated classes, e.g. Spring's 028 * {@link org.springframework.context.annotation.Configuration @Configuration} classes. 029 * 030 * <p>Concrete implementations are required to implement {@link #getRootConfigClasses()} 031 * and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. 032 * Further template and customization methods are provided by 033 * {@link AbstractDispatcherServletInitializer}. 034 * 035 * <p>This is the preferred approach for applications that use Java-based 036 * Spring configuration. 037 * 038 * @author Arjen Poutsma 039 * @author Chris Beams 040 * @since 3.2 041 */ 042public abstract class AbstractAnnotationConfigDispatcherServletInitializer 043 extends AbstractDispatcherServletInitializer { 044 045 /** 046 * {@inheritDoc} 047 * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, 048 * providing it the annotated classes returned by {@link #getRootConfigClasses()}. 049 * Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}. 050 */ 051 @Override 052 protected WebApplicationContext createRootApplicationContext() { 053 Class<?>[] configClasses = getRootConfigClasses(); 054 if (!ObjectUtils.isEmpty(configClasses)) { 055 AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); 056 rootAppContext.register(configClasses); 057 return rootAppContext; 058 } 059 else { 060 return null; 061 } 062 } 063 064 /** 065 * {@inheritDoc} 066 * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, 067 * providing it the annotated classes returned by {@link #getServletConfigClasses()}. 068 */ 069 @Override 070 protected WebApplicationContext createServletApplicationContext() { 071 AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext(); 072 Class<?>[] configClasses = getServletConfigClasses(); 073 if (!ObjectUtils.isEmpty(configClasses)) { 074 servletAppContext.register(configClasses); 075 } 076 return servletAppContext; 077 } 078 079 /** 080 * Specify {@link org.springframework.context.annotation.Configuration @Configuration} 081 * and/or {@link org.springframework.stereotype.Component @Component} classes to be 082 * provided to the {@linkplain #createRootApplicationContext() root application context}. 083 * @return the configuration classes for the root application context, or {@code null} 084 * if creation and registration of a root context is not desired 085 */ 086 protected abstract Class<?>[] getRootConfigClasses(); 087 088 /** 089 * Specify {@link org.springframework.context.annotation.Configuration @Configuration} 090 * and/or {@link org.springframework.stereotype.Component @Component} classes to be 091 * provided to the {@linkplain #createServletApplicationContext() dispatcher servlet 092 * application context}. 093 * @return the configuration classes for the dispatcher servlet application context or 094 * {@code null} if all configuration is specified through root config classes. 095 */ 096 protected abstract Class<?>[] getServletConfigClasses(); 097 098}