001/*
002 * Copyright 2012-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 *      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;
018
019import org.springframework.context.ApplicationContext;
020import org.springframework.context.ConfigurableApplicationContext;
021import org.springframework.core.env.ConfigurableEnvironment;
022import org.springframework.core.io.support.SpringFactoriesLoader;
023
024/**
025 * Listener for the {@link SpringApplication} {@code run} method.
026 * {@link SpringApplicationRunListener}s are loaded via the {@link SpringFactoriesLoader}
027 * and should declare a public constructor that accepts a {@link SpringApplication}
028 * instance and a {@code String[]} of arguments. A new
029 * {@link SpringApplicationRunListener} instance will be created for each run.
030 *
031 * @author Phillip Webb
032 * @author Dave Syer
033 * @author Andy Wilkinson
034 */
035public interface SpringApplicationRunListener {
036
037        /**
038         * Called immediately when the run method has first started. Can be used for very
039         * early initialization.
040         */
041        void starting();
042
043        /**
044         * Called once the environment has been prepared, but before the
045         * {@link ApplicationContext} has been created.
046         * @param environment the environment
047         */
048        void environmentPrepared(ConfigurableEnvironment environment);
049
050        /**
051         * Called once the {@link ApplicationContext} has been created and prepared, but
052         * before sources have been loaded.
053         * @param context the application context
054         */
055        void contextPrepared(ConfigurableApplicationContext context);
056
057        /**
058         * Called once the application context has been loaded but before it has been
059         * refreshed.
060         * @param context the application context
061         */
062        void contextLoaded(ConfigurableApplicationContext context);
063
064        /**
065         * The context has been refreshed and the application has started but
066         * {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner
067         * ApplicationRunners} have not been called.
068         * @param context the application context.
069         * @since 2.0.0
070         */
071        void started(ConfigurableApplicationContext context);
072
073        /**
074         * Called immediately before the run method finishes, when the application context has
075         * been refreshed and all {@link CommandLineRunner CommandLineRunners} and
076         * {@link ApplicationRunner ApplicationRunners} have been called.
077         * @param context the application context.
078         * @since 2.0.0
079         */
080        void running(ConfigurableApplicationContext context);
081
082        /**
083         * Called when a failure occurs when running the application.
084         * @param context the application context or {@code null} if a failure occurred before
085         * the context was created
086         * @param exception the failure
087         * @since 2.0.0
088         */
089        void failed(ConfigurableApplicationContext context, Throwable exception);
090
091}