Disable devtools when running in a native image
See gh-32853
This commit is contained in:
@@ -30,6 +30,7 @@ import org.springframework.boot.devtools.logger.DevToolsLogFactory;
|
||||
import org.springframework.boot.devtools.restart.Restarter;
|
||||
import org.springframework.boot.devtools.system.DevToolsEnablementDeducer;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.NativeDetector;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
@@ -63,19 +64,12 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
|
||||
private static final Map<String, Object> PROPERTIES;
|
||||
|
||||
static {
|
||||
Properties properties = new Properties();
|
||||
try (InputStream stream = DevToolsPropertyDefaultsPostProcessor.class
|
||||
.getResourceAsStream("devtools-property-defaults.properties")) {
|
||||
properties.load(stream);
|
||||
if (NativeDetector.inNativeImage()) {
|
||||
PROPERTIES = Collections.emptyMap();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException("Failed to load devtools-property-defaults.properties", ex);
|
||||
else {
|
||||
PROPERTIES = loadDefaultProperties();
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (String name : properties.stringPropertyNames()) {
|
||||
map.put(name, properties.getProperty(name));
|
||||
}
|
||||
PROPERTIES = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -138,4 +132,24 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Object> loadDefaultProperties() {
|
||||
Properties properties = new Properties();
|
||||
try (InputStream stream = DevToolsPropertyDefaultsPostProcessor.class
|
||||
.getResourceAsStream("devtools-property-defaults.properties")) {
|
||||
if (stream == null) {
|
||||
throw new RuntimeException(
|
||||
"Failed to load devtools-property-defaults.properties because it doesn't exist");
|
||||
}
|
||||
properties.load(stream);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException("Failed to load devtools-property-defaults.properties", ex);
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (String name : properties.stringPropertyNames()) {
|
||||
map.put(name, properties.getProperty(name));
|
||||
}
|
||||
return Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.boot.SpringApplicationAotProcessor;
|
||||
import org.springframework.core.NativeDetector;
|
||||
|
||||
/**
|
||||
* Utility to deduce if DevTools should be enabled in the current context.
|
||||
@@ -47,11 +48,15 @@ public final class DevToolsEnablementDeducer {
|
||||
|
||||
/**
|
||||
* Checks if a specific {@link StackTraceElement} in the current thread's stacktrace
|
||||
* should cause devtools to be disabled.
|
||||
* should cause devtools to be disabled. Devtools will also be disabled if running in
|
||||
* a native image.
|
||||
* @param thread the current thread
|
||||
* @return {@code true} if devtools should be enabled
|
||||
*/
|
||||
public static boolean shouldEnable(Thread thread) {
|
||||
if (NativeDetector.inNativeImage()) {
|
||||
return false;
|
||||
}
|
||||
for (StackTraceElement element : thread.getStackTrace()) {
|
||||
if (isSkippedStackElement(element)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user