冗余声明:@SpringBootApplication已应用给定的@ComponentScan

t9eec4r0  于 2023-01-17  发布在  Spring
关注(0)|答案(1)|浏览(733)

我创建了一个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

InteliJ图片:

如果有人知道原因,请帮助我。如果需要进一步的信息来解决问题,请在这里发表评论。谢谢。

l7wslrjt

l7wslrjt1#

  • 出现以下错误 *:这意味着您可以删除@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

相关问题