/*
 * Copyright 2002-2018 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.servlet.support;

import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

WebApplicationInitializer to register a DispatcherServlet and use Java-based Spring configuration.

Implementations are required to implement:

If an application context hierarchy is not required, applications may return all configuration via getRootConfigClasses() and return null from getServletConfigClasses().

Author:Arjen Poutsma, Chris Beams
Since:3.2
/** * {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer} * to register a {@code DispatcherServlet} and use Java-based Spring configuration. * * <p>Implementations are required to implement: * <ul> * <li>{@link #getRootConfigClasses()} -- for "root" application context (non-web * infrastructure) configuration. * <li>{@link #getServletConfigClasses()} -- for {@code DispatcherServlet} * application context (Spring MVC infrastructure) configuration. * </ul> * * <p>If an application context hierarchy is not required, applications may * return all configuration via {@link #getRootConfigClasses()} and return * {@code null} from {@link #getServletConfigClasses()}. * * @author Arjen Poutsma * @author Chris Beams * @since 3.2 */
public abstract class AbstractAnnotationConfigDispatcherServletInitializer extends AbstractDispatcherServletInitializer {
{@inheritDoc}

This implementation creates an AnnotationConfigWebApplicationContext, providing it the annotated classes returned by getRootConfigClasses(). Returns null if getRootConfigClasses() returns null.

/** * {@inheritDoc} * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, * providing it the annotated classes returned by {@link #getRootConfigClasses()}. * Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}. */
@Override @Nullable protected WebApplicationContext createRootApplicationContext() { Class<?>[] configClasses = getRootConfigClasses(); if (!ObjectUtils.isEmpty(configClasses)) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(configClasses); return context; } else { return null; } }
{@inheritDoc}

This implementation creates an AnnotationConfigWebApplicationContext, providing it the annotated classes returned by getServletConfigClasses().

/** * {@inheritDoc} * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, * providing it the annotated classes returned by {@link #getServletConfigClasses()}. */
@Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); Class<?>[] configClasses = getServletConfigClasses(); if (!ObjectUtils.isEmpty(configClasses)) { context.register(configClasses); } return context; }
Specify @Configuration and/or @Component classes for the root application context.
Returns:the configuration for the root application context, or null if creation and registration of a root context is not desired
/** * Specify {@code @Configuration} and/or {@code @Component} classes for the * {@linkplain #createRootApplicationContext() root application context}. * @return the configuration for the root application context, or {@code null} * if creation and registration of a root context is not desired */
@Nullable protected abstract Class<?>[] getRootConfigClasses();
Specify @Configuration and/or @Component classes for the Servlet application context.
Returns:the configuration for the Servlet application context, or null if all configuration is specified through root config classes.
/** * Specify {@code @Configuration} and/or {@code @Component} classes for the * {@linkplain #createServletApplicationContext() Servlet application context}. * @return the configuration for the Servlet application context, or * {@code null} if all configuration is specified through root config classes. */
@Nullable protected abstract Class<?>[] getServletConfigClasses(); }