001/*
002 * Copyright 2012-2017 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.devtools.restart;
018
019import org.springframework.beans.factory.ObjectFactory;
020import org.springframework.beans.factory.config.Scope;
021import org.springframework.context.ApplicationContextInitializer;
022import org.springframework.context.ConfigurableApplicationContext;
023
024/**
025 * Support for a 'restart' {@link Scope} that allows beans to remain between restarts.
026 *
027 * @author Phillip Webb
028 * @since 1.3.0
029 */
030public class RestartScopeInitializer
031                implements ApplicationContextInitializer<ConfigurableApplicationContext> {
032
033        @Override
034        public void initialize(ConfigurableApplicationContext applicationContext) {
035                applicationContext.getBeanFactory().registerScope("restart", new RestartScope());
036        }
037
038        /**
039         * {@link Scope} that stores beans as {@link Restarter} attributes.
040         */
041        private static class RestartScope implements Scope {
042
043                @Override
044                public Object get(String name, ObjectFactory<?> objectFactory) {
045                        return Restarter.getInstance().getOrAddAttribute(name, objectFactory);
046                }
047
048                @Override
049                public Object remove(String name) {
050                        return Restarter.getInstance().removeAttribute(name);
051                }
052
053                @Override
054                public void registerDestructionCallback(String name, Runnable callback) {
055                }
056
057                @Override
058                public Object resolveContextualObject(String key) {
059                        return null;
060                }
061
062                @Override
063                public String getConversationId() {
064                        return null;
065                }
066
067        }
068
069}