spring - 如何在没有 @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不是特别流利)?
解决方案
您应该能够通过您自己@Import
的类来使用您想要使用的 DevTools 自动配置类。@Configuration
您可能不需要远程支持,在这种情况下,以下内容就足够了:
@Configuration
@Import({LocalDevToolsAutoConfiguration.class, DevToolsDataSourceAutoConfiguration.class})
class ManaulDevToolsConfiguration {
}
万一您想使用自己的 bean 代替 DevTools 的任何自动配置 bean,您需要仔细订购以确保在导入 DevTools 自动配置类之前定义您的 bean并对缺失的 bean 条件进行任何评估。
推荐阅读
- c - 如何在C中释放指向结构的指针数组?
- reactjs - 使用 React 最终形式和 Axios 显示 Api 错误
- asp.net - 从 BaseController 继承的 MVC 控制器后,powershell 运行空间无法打开
- javascript - 为什么我会丢失 JSON.stringify() 方法的属性?
- python - 如何在 Python 3 中将字典转换为列表
- php - 找不到类 php
- android - 如何在 ViewModel 中从图库中选择照片
- azure-data-factory - 如何将变量输出到文件?
- flutter - 有没有办法保留子有状态小部件的状态?
- c# - EF DataContext:代码执行时间过长