没有为作用域名称注册的作用域 'restart'

No Scope registered for scope name 'restart'

我在尝试使用 Spring Boot Devtools 启动应用程序时收到以下堆栈跟踪。

2019-03-15T08:20:26,929 WARN  o.s.c.a.AnnotationConfigApplicationContext:557 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'optionalLiveReloadServer' defined in class path resource [org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration$LiveReloadConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No Scope registered for scope name 'restart'
Exception in thread "restartedMain"
....

这是用于重现问题的 pom 文件。

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dispatch</groupId>
<artifactId>dispatch-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dispatch-java</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

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

<dependencies>
    <!-- Begin Spring Boot dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</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>
</dependencies>

我可以找到人们遇到类似问题但 none 使用 'restart' 范围名称的问题。

我会回答我发现的问题。

似乎是在 Spring 启动应用程序的 main() 方法中加载 ApplicationContext 是导致此问题的原因。

我不得不改变

...
private static ApplicationContext context;

public static void main(final String[] args) {

    context = new AnnotationConfigApplicationContext(DispatcherConfiguration.class);

    SpringApplication.run(DispatcherApplication.class, args);
}

public static ApplicationContext getApplicationContext() {
    return context;
}

一旦我停止设置 context Spring 使用 Dev Tools 启动正常。

检查@ComponentScan是否只用了一次。在我的例子中,我在 SpringMainApplication.java(其中有 main 方法)和 BeanDecleration.java 文件中使用了 @ComponentScan。从 BeanDecleration.java 文件中删除 @ComponentScan 后,错误得到解决。