Java 没有为作用域名称注册作用域';重新启动&x27;

Java 没有为作用域名称注册作用域';重新启动&x27;,java,spring,spring-boot-devtools,Java,Spring,Spring Boot Devtools,在尝试使用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.BeanCreation

在尝试使用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.0.0
派遣
调度java
0.0.1-快照
调度java
org.springframework.boot
spring启动程序父级
2.1.3.1发布
1.8
org.springframework.boot
springbootmaven插件
org.springframework.boot
弹簧靴起动器
org.springframework.boot
spring启动程序日志记录
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
弹簧靴开发工具
运行时
真的
org.springframework.boot
弹簧起动试验
测试

我可以找到人们有类似问题但没有“重启”作用域名称的问题


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

似乎在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启动就可以用开发工具正常启动。

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

之前和之后的情况如何。(你到底改变了什么?)