java - 没有为范围名称 'restart' 注册范围

标签 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.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>

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

我会回答我发现的问题。

最佳答案

看来,在 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 就可以通过开发工具正常启动。

关于java - 没有为范围名称 'restart' 注册范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55183979/

相关文章:

java - 订购 Google Cloud Pub/Sub 消息 - java 示例程序

java - 从 Activity 获取值到自定义数组适配器

java - 如何在实体中制作唯一字​​段,但相对多对一实体?

Spring-Boot-Devtools 不想重新加载进程(没有选择正确的类路径?)

java - 解析 .p7m 文件时缺少附件

Java logging API MemoryHandler 即时转储日志

java - 如何使用serialize Collections.synchronizedList和co

spring - 在请求参数 'null' 或 header '_csrf' 上发现无效的 CSRF token 'X-CSRF-TOKEN'

java - Spring Boot 开发工具在 Netbeans 中不起作用

java - Spring Boot DevTools 在 Eclipse 中不工作