【问题标题】:Redundant declaration: @SpringBootApplication already applies given @ComponentScan冗余声明:@SpringBootApplication 已经适用给定的@ComponentScan
【发布时间】:2023-01-16 15:57:40
【问题描述】:

我创建了一个 spring boot 应用程序(3.0.1)。然后我连接了 Postgres 数据库并执行了它。应用在8080服务器上顺利启动。然后我添加了下面提到的注释。

@EnableAutoConfiguration
@ComponentScan

整个文件,

package com.example.SpringRecap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan

public class SpringRecapApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringRecapApplication.class, args);
    }

    @GetMapping("/")
    public void Employee() {

    }


}

现在,出现下面提到的错误,

Redundant declaration: @SpringBootApplication already applies @EnableAutoConfiguration
Redundant declaration: @SpringBootApplication already applies given @ComponentScan

IntelliJ 的图像:

如果有人知道原因,请帮助我。如果需要更多信息来解决问题,请在此处发表评论。 谢谢你。

【问题讨论】:

  • @Jens 不,它显示错误,等等,我会放一张截图,我认为你只是在将其视为警告之后才对我投反对票。

标签: java spring-boot annotations spring-boot-maven-plugin component-scan


【解决方案1】:

出现下面提到的错误:这意味着您可以删除@ComponentScan@EnableAutoConfiguration

As the documentation on spring mentioned,包含在@SpringBootApplication中:

Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single @SpringBootApplication annotation can be used to enable those three features, that is:

    @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
    @ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
    @Configuration: allow to register extra beans in the context or import additional configuration classes 

这是Intellij插件中的检查。另见https://youtrack.jetbrains.com/issue/IDEA-177632

【讨论】:

  • 是的,我可以删除它,但我正在寻找出现错误的理由。因为对于某些人来说它工作正常。你知道原因吗?
  • 我想这是从 IDE 检查插件
  • 哇,太棒了。感谢@Jens 的支持。现在我可以接受你的回答了。
猜你喜欢
  • 1970-01-01
  • 2012-10-24
  • 2021-04-08
  • 2011-10-20
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多