From 90545fb0c61338ee545cac7c5587d95baf869767 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sat, 27 Jan 2018 05:55:56 +0100 Subject: [PATCH] Update LocalDevToolsAutoConfiguration to use constructor injection Closes gh-11769 --- .../LocalDevToolsAutoConfiguration.java | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java index 2e0fbe6184..5e3e98a89f 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import java.io.File; import java.net.URL; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -62,11 +61,11 @@ public class LocalDevToolsAutoConfiguration { @ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "enabled", matchIfMissing = true) static class LiveReloadConfiguration { - @Autowired - private DevToolsProperties properties; + private final DevToolsProperties properties; - @Autowired(required = false) - private LiveReloadServer liveReloadServer; + LiveReloadConfiguration(DevToolsProperties properties) { + this.properties = properties; + } @Bean @RestartScope @@ -76,21 +75,16 @@ public class LocalDevToolsAutoConfiguration { Restarter.getInstance().getThreadFactory()); } - @EventListener - public void onContextRefreshed(ContextRefreshedEvent event) { - optionalLiveReloadServer().triggerReload(); - } - - @EventListener - public void onClassPathChanged(ClassPathChangedEvent event) { - if (!event.isRestartRequired()) { - optionalLiveReloadServer().triggerReload(); - } + @Bean + public OptionalLiveReloadServer optionalLiveReloadServer( + LiveReloadServer liveReloadServer) { + return new OptionalLiveReloadServer(liveReloadServer); } @Bean - public OptionalLiveReloadServer optionalLiveReloadServer() { - return new OptionalLiveReloadServer(this.liveReloadServer); + public LiveReloadServerEventListener liveReloadServerEventListener( + OptionalLiveReloadServer liveReloadServer) { + return new LiveReloadServerEventListener(liveReloadServer); } } @@ -102,8 +96,11 @@ public class LocalDevToolsAutoConfiguration { @ConditionalOnProperty(prefix = "spring.devtools.restart", name = "enabled", matchIfMissing = true) static class RestartConfiguration { - @Autowired - private DevToolsProperties properties; + private final DevToolsProperties properties; + + RestartConfiguration(DevToolsProperties properties) { + this.properties = properties; + } @EventListener public void onClassPathChanged(ClassPathChangedEvent event) { @@ -164,4 +161,27 @@ public class LocalDevToolsAutoConfiguration { } + static class LiveReloadServerEventListener { + + private final OptionalLiveReloadServer liveReloadServer; + + LiveReloadServerEventListener( + OptionalLiveReloadServer liveReloadServer) { + this.liveReloadServer = liveReloadServer; + } + + @EventListener + public void onContextRefreshed(ContextRefreshedEvent event) { + this.liveReloadServer.triggerReload(); + } + + @EventListener + public void onClassPathChanged(ClassPathChangedEvent event) { + if (!event.isRestartRequired()) { + this.liveReloadServer.triggerReload(); + } + } + + } + }