1

尝试使用 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>

我可以找到人们遇到类似问题但没有“重启”范围名称的问题。

我会用我发现的问题来回答。

4

2 回答 2

2

似乎在 Spring Boot 应用程序的 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;
}

一旦我停止设置上下文,Spring Boot 就可以使用 Dev Tools 正常启动。

于 2019-03-15T13:44:01.840 回答
1

检查是否@ComponentScan只使用一次。在我的情况下,我@ComponentScanSpringMainApplication.java(那里有 main 方法)和BeanDecleration.java文件中使用过。@ComponentScan从文件中删除后BeanDecleration.java,错误得到解决。

于 2020-03-23T14:29:30.377 回答