Spring Boot : Disable AutoConfiguration of all modules - spring-boot

In Spring Boot, is there a way to prevent Auto Configuration of all modules? Basically am looking for something like #DisableAutoConfiguration instead of excluding specific configurations with class names.

Auto-configuration is enabled by the #EnableAutoConfiguration annotation. If you don't want to use auto-configuration, then omit this annotation. Note that #SpringBootApplication is itself annotated with #EnableAutoConfiguration so you'll have to avoid using it too. Typically, this would leave your main application class annotated with #ComponentScan and #Configuration.

Related

Should Spring Boot auto-configuration classes use the #Configuration annotation?

Based on my testing, I've found no obvious difference when Spring Boot auto-configuration classes have or don't have the #Configuration annotation - if they are configured in spring.factories correctly they are loaded regardless of the #Configuration annotation.
However, it seems like every custom auto-configuration example and demo uses the #Configuration annotation. Is there a reason all these examples use #Configuration (or is it just convention)? Is there any impact to not using #Configuration?
Some examples that use #Configuration on auto-configuration classes:
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration.condition-annotations.class-conditions
https://www.baeldung.com/spring-boot-custom-auto-configuration#creating-a-custom-auto-configuration
https://www.baeldung.com/spring-boot-custom-starter#1-auto-configuration-classes
https://www.javadevjournal.com/spring-boot/spring-boot-custom-starter/
https://billykorando.com/2019/12/30/building-a-custom-spring-boot-starter/
http://www.masterspringboot.com/getting-started-with-spring-boot/spring-boot-quickstarts/how-to-build-a-custom-spring-boot-starter-in-no-time/
https://github.com/snicoll/spring-boot-master-auto-configuration/blob/main/hornetq-spring-boot-autoconfigure/src/main/java/hornetq/autoconfigure/HornetQAutoConfiguration.java
Yes, they should be. The documentation states that an auto-configuration class should be annotated with #Configuration:
Under the hood, auto-configuration is implemented with standard #Configuration classes. Additional #Conditional annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use #ConditionalOnClass and #ConditionalOnMissingBean annotations.
If you do not annotate them with #Configuration you are relying upon Spring Framework’s “lite” #Bean mode. This changes the behaviour of the auto-configuration classes. It is not guaranteed to work in the future as the implementation does not meet the documented requirements.

Does #ComponentScan override #SpringBootApplication?

My #SpringBootApplication annotation is present in com.abc.def package.
According to this article , using #SpringBootApplication annotation is equivalent to using #Configuration, #EnableAutoConfiguration, and #ComponentScan with their default attributes :-
https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html
If i supply my own #ComponentScan, does it add new packages to the default value or completely override the default ?
#SpringBootApplication annotation can component scan classes from packages under Application class belong to . Also you can add #ComponentScan to scan classes not under package Application class belongs to.
#ComponentScan("external.pkg")
#SpringBootApplication
class MyApp{
}
the annotation #SpringBootApplication contains #EnableAutoConfiguration annotation, which will scan through your jar, class, and maven dependencies in the classpath to register beans. If you declare beans cross packages and only want to use specific beans from specific packages, then use #ComponentScan
#ComponentScan gives you a fine-grained package or class level control on what beans you declared would be registered.
The #SpringBootApplication annotation doesn't allow all the filter customizations supported by #ComponentScan. We observed in our application that spring boot was automatically loading a #Configuration class from a library outside the package hierarchy of the class annotated with #SpringBootApplication.
If you want more control, one idea is to omit the #SpringBootApplication annotation and just specify the individual annotations #Configuration, #EnableAutoConfiguration, and #ComponentScan with your desired customizations.
More information is available in Spring Boot's Documentation:
18. Using the #SpringBootApplication Annotation
Good luck

Difference between #EnableAutoConfiguration vs #SpringBootConfiguration ( #AutoConfigurationPackage vs #Configuration )

I know that when we annotate a java class as #SpringBootApplication we will have internally annotations #EnableAutoConfiguration and #SpringBootConfiguration but i'm confused what is the difference between them.
I am very much new to spring boot, Can someone please elaborate on this.
public #interface SpringBootConfiguration
Indicates that a class provides Spring Boot application
#Configuration. Can be used as an alternative to the Spring's standard
#Configuration annotation so that configuration can be found
automatically (for example in tests).
from: SpringBootConfiguration docs
public #interface EnableAutoConfiguration
Enable auto-configuration of the Spring Application Context,
attempting to guess and configure beans that you are likely to need.
from: EnableAutoConfiguration docs
So what is the difference?
#SpringBootConfiguration annotation tells us that a class is a configuration class, and #EnableAutoConfiguration automatically configures the Spring application based on its included jar files.
it is meta annotation #SpringBootApplication will have other annotations
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java
if #SpringBootApplication is not there applications need to use other annotations on Main class

Enable Actuator health endpoint without enabling auto config

In my project, I don't want to use #EnableAutoConfiguration. My Application.java has #ComponentScan, #Configuration and #Import annotation.
I have added spring boot actuator dependency in my pom.xml. But, when I try to access http://<>/acutuator/health, I get 404. I believe I need to specify some config class as part of Import annotation. I would need help in figuring out what that config would be.
#EnableAutoConfiguration makes Spring guess configuration based on the classpath, and that's what spring boot all about. If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of #EnableAutoConfiguration to disable them. For example:
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

What is SpringBootApplication functionality?

I understand that #SpringBootApplication is used to mark the main class of a spring application.
Here I see that it "is equivalent to using #Configuration, #EnableAutoConfiguration and #ComponentScan". But why is there needed a #Configuration if it is already made automatically with #EnableAutoConfiguration.
#EnableAutoConfiguration means Spring boot will look what libraries you use and try to set up beans accordingly. It has nothing to do with #Configuration.

Resources