【发布时间】:2021-04-19 12:03:12
【问题描述】:
编写了我的第一个计算刷新次数的 Spring Boot 应用程序。我打印到端口 8080 "Hello World" + Counter。
计数器应在每次用户刷新时增加,并在“Hello World”字符串后实时显示刷新次数。
为此,我使用@RestController 和@EventListener 接口。
在构建我的应用时,@EventListener 导入和接口调用失败:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project backend: Compilation failure: Compilation failure:
[ERROR] /home/project/backend/src/main/java/com/iz/backend/myController.java:[9,58] error: cannot find symbol
[ERROR] package org.springframework.cloud.context.config.annotation
[ERROR] /home/project/backend/src/main/java/com/iz/backend/myController.java:[57,5] error: incompatible types: EventListener cannot be converted to Annotation
类:
package com.iz.backend;
import org.springframework.stereotype.Controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.config.annotation.EventListener;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
@RefreshScope
@RestController //interface
public class myController {
static int counter = -1;
@RequestMapping("/")
public String helloWorld() {
try
{
return "Hello World " + counter;
}
catch (IOException e)
{
e.printStackTrace();
}
return "Failed to execute";
}
@EventListener
public void handleContextRefreshEvent(ContextRefreshedEvent event) {
counter += 1;
}
}
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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.iz</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>backend</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
MAVEN 3.6.3,JAVA 11
【问题讨论】:
-
EventListener有两个导入。删除一个(可能是java.util.EventListener)。 -
我不小心把这一行加到了这里。现已编辑。在我的实际代码中 java.util.EventListener 不存在。
标签: java spring-boot maven pom.xml