Redundant declaration: @SpringBootApplication already applies given @ComponentScan

Advertisements

I created a spring boot application(3.0.1). Then I connected the Postgres database and executed it. The application is up on server 8080 smoothly. then I added below-mentioned annotations.

@EnableAutoConfiguration
@ComponentScan

whole file,

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() {

    }


}

Now, below mentioned error are appeared,

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

Image of InteliJ:

If anyone knows the reason, please help me. If further information is needed to solve the problem please put a comment here.
Thank You.

>Solution :

below mentioned error are appeared: This means that you can remove @ComponentScan and @EnableAutoConfiguration.

As the documentation on spring mentioned, it is included in @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 

It is a check in the plugin of Intellij. Also see https://youtrack.jetbrains.com/issue/IDEA-177632

Leave a ReplyCancel reply