Spring Boot devtools - 有时重新加载的 bean 在重启时没有被拾取

Spring Boot devtools - sometimes reloaded bean is not picked up on restart

我在使用 spring-devtools 重启功能时遇到了一些问题。似乎有时由于不明原因更改并重新加载的 bean 未被拾取。

申请class:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

命令行运行程序:

@Component
@RequiredArgsConstructor
@Slf4j
public class TestCommandLineRunner implements CommandLineRunner {
    private final TestService testService;

    @Override
    public void run(String... args) throws Exception {
        log.info("{}", testService.test());
    }
}

更改并重新加载的服务:

@Service
public class TestService {
    public int test() {
        return 12; //I am changing this number for tests
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chrosciu</groupId>
    <artifactId>devtools-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>devtools-test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

通常第一次重新加载工作正常:

2019-09-15 12:08:31.788  INFO 16464 --- [       Thread-5] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-09-15 12:08:32.121  INFO 16464 --- [  restartedMain] com.chrosciu.DemoApplication             : Starting DemoApplication on chma1 with PID 16464 (C:\Users\chma\gitroot\devtools-test\target\classes started by chma in C:\Users\chma\gitroot\devtools-test)
2019-09-15 12:08:32.122  INFO 16464 --- [  restartedMain] com.chrosciu.DemoApplication             : No active profile set, falling back to default profiles: default
2019-09-15 12:08:32.663  INFO 16464 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-15 12:08:32.664  INFO 16464 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-15 12:08:32.666  INFO 16464 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-15 12:08:32.725  INFO 16464 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-15 12:08:32.725  INFO 16464 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 600 ms
2019-09-15 12:08:32.833  INFO 16464 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-15 12:08:32.900  INFO 16464 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-09-15 12:08:32.924  INFO 16464 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-15 12:08:32.925  INFO 16464 --- [  restartedMain] com.chrosciu.DemoApplication             : Started DemoApplication in 0.86 seconds (JVM running for 18.403)
2019-09-15 12:08:32.926  INFO 16464 --- [  restartedMain] com.chrosciu.TestCommandLineRunner       : 8
2019-09-15 12:08:32.927  INFO 16464 --- [  restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged

但是第二个或第三个崩溃如下,并显示有关缺少 bean 的信息:

2019-09-15 12:09:15.482  INFO 16464 --- [      Thread-21] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-09-15 12:09:15.842  INFO 16464 --- [  restartedMain] com.chrosciu.DemoApplication             : Starting DemoApplication on chma1 with PID 16464 (C:\Users\chma\gitroot\devtools-test\target\classes started by chma in C:\Users\chma\gitroot\devtools-test)
2019-09-15 12:09:15.843  INFO 16464 --- [  restartedMain] com.chrosciu.DemoApplication             : No active profile set, falling back to default profiles: default
2019-09-15 12:09:16.396  INFO 16464 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-15 12:09:16.397  INFO 16464 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-15 12:09:16.398  INFO 16464 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-15 12:09:16.483  INFO 16464 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-15 12:09:16.483  INFO 16464 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 637 ms
2019-09-15 12:09:16.714  WARN 16464 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testCommandLineRunner' defined in file [C:\Users\chma\gitroot\devtools-test\target\classes\com\chrosciu\TestCommandLineRunner.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.chrosciu.TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2019-09-15 12:09:16.714  INFO 16464 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-09-15 12:09:16.723  INFO 16464 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-15 12:09:16.969 ERROR 16464 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.chrosciu.TestCommandLineRunner required a bean of type 'com.chrosciu.TestService' that could not be found.


Action:

Consider defining a bean of type 'com.chrosciu.TestService' in your configuration.

有人知道发生了什么事吗?任何帮助表示赞赏。

可能是无论您使用什么来编译所做的更改,都是删除 class 文件,然后重新编译源代码,而不是将新的 class 文件写在旧的。如果 class 文件被删除和源代码被重新编译之间的时间大于 DevTools 的静默期,应用程序将在没有更改 class 的情况下重新启动 – DevTools 会认为您已删除它而不是改变它。

您可能想要增加静默期,以便 DevTools 在同一批次中看到删除和创建重新编译的 class 文件。默认静默期为 400 毫秒。以下示例将其设置为 1 秒:

spring.devtools.restart.quiet-period=1s

您可以在 relevant section of the documentation 中了解有关 DevTools 静默期的更多信息以及如何配置它。