Skip to content

Commit 2bd3d19

Browse files
committed
Ensure that DevTools' beans use eager init even when app is lazy
Closes gh-15870
1 parent 1431a0f commit 2bd3d19

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

spring-boot-project/spring-boot-devtools/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ org.springframework.boot.devtools.logger.DevToolsLogFactory.Listener
99

1010
# Auto Configure
1111
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
12+
org.springframework.boot.devtools.autoconfigure.EagerInitializationAutoConfiguration,\
1213
org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration,\
1314
org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration,\
1415
org.springframework.boot.devtools.autoconfigure.RemoteDevToolsAutoConfiguration

0 commit comments

Comments
 (0)