首页 > 解决方案 > 如何在没有 @EnableAutconfiguration 或 @SpringBootApplication 的情况下使 spring-boot-devtools 工作?

问题描述

我正在使用 spring-boot v1.3.5.RELEASE 开发一个应用程序,它不使用@SpringBootApplication也不@EnableAutconfiguration(客户要求)。

我想spring-boot-devtools在我的 IDE 中启用“重启”功能。我把它作为依赖,打mvn spring-boot:run。重启器工作:

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/...]

但是在 IDE 代码修改和 rebluid 后它不会重新加载(在 eclipse 上按 Ctrl+B)。

问题似乎是 devtools 依赖于@EnableAutconfiguration(加载了 的工厂META-INF/spring.factories)进行配置(我不能使用这个注释)。基本上,我需要自己做(见下面的 devtoolsspring.factories文件的内容):

# 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

我该怎么做(我对spring-boot lingua不是特别流利)?

标签: springspring-bootspring-boot-devtools

解决方案


您应该能够通过您自己@Import的类来使用您想要使用的 DevTools 自动配置类。@Configuration您可能不需要远程支持,在这种情况下,以下内容就足够了:

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

}

万一您想使用自己的 bean 代替 DevTools 的任何自动配置 bean,您需要仔细订购以确保在导入 DevTools 自动配置类之前定义您的 bean并对缺失的 bean 条件进行任何评估。


推荐阅读