|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.devtools.autoconfigure; |
| 18 | + |
| 19 | +import org.springframework.beans.BeansException; |
| 20 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 21 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| 22 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 23 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.core.Ordered; |
| 26 | + |
| 27 | +/** |
| 28 | + * {@link EnableAutoConfiguration Auto-configuration} to ensure that DevTools' beans are |
| 29 | + * eagerly initialized when the application is otherwise being initialized lazily. |
| 30 | + * |
| 31 | + * @author Andy Wilkinson |
| 32 | + * @since 2.2.0 |
| 33 | + */ |
| 34 | +public class EagerInitializationAutoConfiguration { |
| 35 | + |
| 36 | + @Bean |
| 37 | + public static AlwaysEagerBeanFactoryPostProcessor alwaysEagerBeanFactoryPostProcessor() { |
| 38 | + return new AlwaysEagerBeanFactoryPostProcessor(); |
| 39 | + } |
| 40 | + |
| 41 | + private static final class AlwaysEagerBeanFactoryPostProcessor |
| 42 | + implements BeanFactoryPostProcessor, Ordered { |
| 43 | + |
| 44 | + @Override |
| 45 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) |
| 46 | + throws BeansException { |
| 47 | + for (String name : beanFactory.getBeanDefinitionNames()) { |
| 48 | + BeanDefinition beanDefinition = beanFactory.getBeanDefinition(name); |
| 49 | + String factoryBeanName = beanDefinition.getFactoryBeanName(); |
| 50 | + if (factoryBeanName != null && factoryBeanName |
| 51 | + .startsWith("org.springframework.boot.devtools")) { |
| 52 | + beanDefinition.setLazyInit(false); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public int getOrder() { |
| 59 | + return Ordered.HIGHEST_PRECEDENCE + 1; |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments