0

I'm working on an application using spring-boot v1.3.5.RELEASE, which doesn't use @SpringBootApplication nor @EnableAutconfiguration (customer requirement).

I want to enable spring-boot-devtools to have the "restart" feature in my IDE. I've put it as dependency, hit mvn spring-boot:run. the restarter works:

DEBUG o.s.b.d.r.Restarter - Creating new Restarter for thread Thread[main,5,main]
DEBUG o.s.b.d.r.Restarter - Immediately restarting application
DEBUG o.s.b.d.r.Restarter - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@22843e39
DEBUG o.s.b.d.r.Restarter - Starting application application.Application with URLs [file:/D:/git/repos/...]

But it doesn't reload after IDE code modification and rebluid (Ctrl+B on eclipse).

The problem seems to be that devtools relies on @EnableAutconfiguration (factories loaded with the META-INF/spring.factories) to be configured (and I can't use this annotation). Basically, I need to do that by myself (see below the content of the devtools spring.factories file):

# Application Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.devtools.restart.RestartScopeInitializer
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.devtools.restart.RestartApplicationListener
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration,\
org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration,\
org.springframework.boot.devtools.autoconfigure.RemoteDevToolsAutoConfiguration
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.devtools.env.DevToolsHomePropertiesPostProcessor,\
org.springframework.boot.devtools.env.DevToolsPropertyDefaultsPostProcessor
# Restart Listeners
org.springframework.boot.devtools.restart.RestartListener=\
org.springframework.boot.devtools.log4j2.Log4J2RestartListener

How can I do that (I'm not particulary fluent in spring-boot lingua) ?

Tusc
  • 1,912
  • 1
  • 11
  • 8

1 Answers1

1

You should be able to @Import the DevTools auto-configuration classes that you want to use via a @Configuration class of your own. You may not want the remote support, in which case the following should suffice:

@Configuration
@Import({LocalDevToolsAutoConfiguration.class, DevToolsDataSourceAutoConfiguration.class})
class ManaulDevToolsConfiguration {

}

In the unlikely event that you want to use a bean of your own in place of any of DevTools' auto-configured beans, you'll need to order things carefully to ensure that your beans are defined before the DevTools auto-configuration classes are imported and the evaluation of any on missing bean conditions is performed.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Thank you for your response. I've tried the solution but it doesn't work: It seems that the restarter feature needs also the application initializer and listeners to work. – Tusc Dec 02 '19 at 09:24
  • So I've made a spring "profile" and added in properties "context.initializer.classes" and "context.listener.classes". That's doing the trick. But I have no solutions for Environment post processors (need this to overload "spring-devtools.properties" in my HOME) and restart listener (don't know if it is useful - we use log4j in our app). Have you an hint for these (register env. post processor & restart listener) ? That would be great. – Tusc Dec 02 '19 at 09:36
  • They should be loaded without using auto-configuration via `SpringApplication` or `SpringApplicationBuilder`. – Andy Wilkinson Dec 02 '19 at 10:29
  • I agree with you for `ApplicationContextInitializer` and `ApplicationListener` (I used properties because on this project I can't modify `SpringApplication`), but for the others (`EnvironmentPostProcessor` and `RestartListener`), I'm stuck... – Tusc Dec 02 '19 at 11:19