== Why is this an issue?

"@EnableAutoConfiguration" is a convenient feature to configure the Spring Application Context by attempting to guess the beans that you are likely to need. The drawback is that it may load and configure beans the application will never use and therefore consume more CPU and RAM than really required. ``++@EnableAutoConfiguration++`` should be configured to exclude all the beans not required by the application. Alternatively, use the ``++@Import++`` annotation instead of ``++@EnableAutoConfiguration++``, to explicitly import the useful AutoConfiguration classes.

This rule applies for ``++@SpringBootApplication++`` as well.


=== Noncompliant code example

[source,java]
----
@SpringBootApplication
public class MyApplication {
...
}
----

[source,java]
----
@Configuration
@EnableAutoConfiguration
public class MyApplication {
...
}
----


=== Compliant solution

[source,java]
----
@SpringBootApplication(exclude = {
  MultipartAutoConfiguration.class,
  JmxAutoConfiguration.class,
})
public class MyApplication {
...
}
----

[source,java]
----
@Configuration
@EnableAutoConfiguration(exclude = {
  MultipartAutoConfiguration.class,
  JmxAutoConfiguration.class,
})
public class MyApplication {
...
}
----

[source,java]
----
@Configuration
@Import({
        DispatcherServletAutoConfiguration.class,
        EmbeddedServletContainerAutoConfiguration.class,
        ErrorMvcAutoConfiguration.class,
        HttpEncodingAutoConfiguration.class,
        HttpMessageConvertersAutoConfiguration.class,
        JacksonAutoConfiguration.class,
        ServerPropertiesAutoConfiguration.class,
        PropertyPlaceholderAutoConfiguration.class,
        ThymeleafAutoConfiguration.class,
        WebMvcAutoConfiguration.class
})
public class MyApplication {
...
}
----


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Exclude from the auto-configuration mechanism the beans you don't need.


'''
== Comments And Links
(visible only on this page)

=== on 20 Apr 2018, 13:58:35 Alexandre Gigleux wrote:
Reference: \https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html

=== on 13 Jun 2018, 17:55:11 Andrei Epure wrote:
Could this produce many False Positives? How can we tell if there is something to exclude or not?


At least in https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html#using-boot-disabling-specific-auto-configuration[the official docs], there's no recommendation to exclude. It's done on a per-need basis, and that need is quite context-specific...

=== on 13 Jun 2018, 18:02:25 Alexandre Gigleux wrote:
This rule was inspired by \https://www.alexecollins.com/spring-boot-performance/

=== on 14 Mar 2021, 10:30:46 Stéphane Nicoll wrote:
Hello,


Spring Boot developer here. The way the rule is described is quite misleading IMO, I'd be curious to understand what led you (besides that blog post) to do this.


This rule sounds like it is a mistake to have a Spring Boot application with ``++@SpringBootApplication. ++``It is the general recommended approach for most apps.


> it may load and configure beans the application will never use and therefore consume more CPU and RAM than really required.


There's quite a gap between "it may" and not recommending users to use that feature at all. A major code smell sounds like something users should pay attention. IMO, the rule the way it is described will point users in the wrong direction.


As for the recommendation of using `@Import`, that's actually inaccurate. Auto-configurations are processed at a specific point of the lifecycle of the application context and using `@Import` imports them in the user config. It can be ok for corner cases (like enabling a specific feature when it was disabled based on a condition) but that's about it. `@ImportAutoConfiguration` exists but is mostly meant for testing purposes.

endif::env-github,rspecator-view[]
