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.servlet.support;
018
019import org.springframework.lang.Nullable;
020import org.springframework.util.ObjectUtils;
021import org.springframework.web.context.WebApplicationContext;
022import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
023
024/**
025 * {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}
026 * to register a {@code DispatcherServlet} and use Java-based Spring configuration.
027 *
028 * <p>Implementations are required to implement:
029 * <ul>
030 * <li>{@link #getRootConfigClasses()} -- for "root" application context (non-web
031 * infrastructure) configuration.
032 * <li>{@link #getServletConfigClasses()} -- for {@code DispatcherServlet}
033 * application context (Spring MVC infrastructure) configuration.
034 * </ul>
035 *
036 * <p>If an application context hierarchy is not required, applications may
037 * return all configuration via {@link #getRootConfigClasses()} and return
038 * {@code null} from {@link #getServletConfigClasses()}.
039 *
040 * @author Arjen Poutsma
041 * @author Chris Beams
042 * @since 3.2
043 */
044public abstract class AbstractAnnotationConfigDispatcherServletInitializer
045                extends AbstractDispatcherServletInitializer {
046
047        /**
048         * {@inheritDoc}
049         * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
050         * providing it the annotated classes returned by {@link #getRootConfigClasses()}.
051         * Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}.
052         */
053        @Override
054        @Nullable
055        protected WebApplicationContext createRootApplicationContext() {
056                Class<?>[] configClasses = getRootConfigClasses();
057                if (!ObjectUtils.isEmpty(configClasses)) {
058                        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
059                        context.register(configClasses);
060                        return context;
061                }
062                else {
063                        return null;
064                }
065        }
066
067        /**
068         * {@inheritDoc}
069         * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
070         * providing it the annotated classes returned by {@link #getServletConfigClasses()}.
071         */
072        @Override
073        protected WebApplicationContext createServletApplicationContext() {
074                AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
075                Class<?>[] configClasses = getServletConfigClasses();
076                if (!ObjectUtils.isEmpty(configClasses)) {
077                        context.register(configClasses);
078                }
079                return context;
080        }
081
082        /**
083         * Specify {@code @Configuration} and/or {@code @Component} classes for the
084         * {@linkplain #createRootApplicationContext() root application context}.
085         * @return the configuration for the root application context, or {@code null}
086         * if creation and registration of a root context is not desired
087         */
088        @Nullable
089        protected abstract Class<?>[] getRootConfigClasses();
090
091        /**
092         * Specify {@code @Configuration} and/or {@code @Component} classes for the
093         * {@linkplain #createServletApplicationContext() Servlet application context}.
094         * @return the configuration for the Servlet application context, or
095         * {@code null} if all configuration is specified through root config classes.
096         */
097        @Nullable
098        protected abstract Class<?>[] getServletConfigClasses();
099
100}