【问题标题】:How can I make spring-boot-devtools work without @EnableAutconfiguration or @SpringBootApplication?如何在没有 @EnableAutconfiguration 或 @SpringBootApplication 的情况下使 spring-boot-devtools 工作?
【发布时间】:2019-11-29 17:57:23
【问题描述】:

我正在使用 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)来配置(我不能使用这个注解)。 基本上,我需要自己做(见下面的 devtools spring.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 不是特别流利)?

【问题讨论】:

    标签: spring spring-boot spring-boot-devtools


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复。我已经尝试过解决方案,但它不起作用:似乎重启器功能还需要应用程序初始化程序和侦听器才能工作。
    • 所以我制作了一个弹簧“配置文件”并添加了属性“context.initializer.classes”和“context.listener.classes”。这就是诀窍。但是我没有环境后处理器的解决方案(需要这个来重载我家中的“spring-devtools.properties”)并重新启动监听器(不知道它是否有用 - 我们在我们的应用程序中使用 log4j)。您对这些有提示吗(注册环境。后处理器和重新启动侦听器)?那太好了。
    • 它们应该在不使用自动配置的情况下通过SpringApplicationSpringApplicationBuilder 加载。
    • 我同意你对ApplicationContextInitializerApplicationListener 的看法(我使用属性是因为在这个项目中我不能修改SpringApplication),但对于其他人(EnvironmentPostProcessorRestartListener ),我被困住了……
    猜你喜欢
    • 2020-10-21
    • 2020-04-03
    • 2019-10-29
    • 2016-01-22
    • 2020-12-23
    • 2017-07-22
    • 2019-03-17
    • 2020-12-20
    • 2016-09-20
    相关资源
    最近更新 更多