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

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.

Related

What does it mean by #SpringBootConfiguration allows the configuration to be found automatically?

I was searching for the difference between the #Configuration vs #SpringBootConfiguration. I found many articles mentioned "As per the Spring documentation, #SpringBootConfiguration is just an alternative to the Spring standard #Configuration annotation. The only difference between the two is that the #SpringBootConfiguration allows the configuration to be found automatically".
So what does it mean by #SpringBootConfiguration allows the configuration to be found automatically? If we use the #Configuration for a class that contains #Bean annotated methods, Spring will create instantiation for those beans as well. Can anybody explain this to me clearly?
Because this question was driving me a little crazy and this was the first StackOverflow page I found, which didn't resolve the issue for me, I'm going to try to explain my findings:
#SpringBootApplication is a specialisation of #SpringBootConfiguration, which is a specialisation of #Configuration.
You may only have 1 #SpringBootConfiguration, which means you can't have both a #SpringBootConfiguration and a #SpringBootApplication in your application.
"#SpringBootConfiguration allows the configuration to be found automatically" means you don't have to do anything to have this (and therefore #SpringBootApplication) found. Other #Configuration classes are generally discovered by #ComponentScan
An extra advantage over #Configuration, is that it will be discovered by #StringBootTest.
Just open https://github.dev/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-docs/src/docs/asciidoc and search for SpringBootConfiguration you will find some information about it.
such as
#SpringBootConfiguration: enable registration of extra beans in the
context or the import of additional configuration classes. An
alternative to Spring’s standard #Configuration that aids
configuration detection in your integration tests.
and
Detecting Test Configuration If you are familiar with the Spring Test
Framework, you may be used to using #ContextConfiguration(classes=…​)
in order to specify which Spring #Configuration to load.
Alternatively, you might have often used nested #Configuration classes
within your test.
When testing Spring Boot applications, this is often not required.
Spring Boot’s #*Test annotations search for your primary configuration
automatically whenever you do not explicitly define one.
The search algorithm works up from the package that contains the test
until it finds a class annotated with #SpringBootApplication or
#SpringBootConfiguration. As long as you structured your code in a
sensible way, your main configuration is usually found.

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

How SpringBoot dependency injection works with different type of annotations

I recently started exploring Spring Boot. I see that there are 2 ways to define Beans in Spring Boot.
Define #Bean in the class annotated with #SprinBootApplication
Define #Bean in a class annotated with #Configuration
I am also confused about stereo-type annotation #Repository #Service #Controller etc.
Can someone please explain how dependency-injection works with these annotations?
Yes it is possible.
Either you use #Bean in any of your #Configuration or #SpringBootApplication class or mark the bean classes explicitly with annotations like #Service, #Component #Repository etc.
#Service or #Component
When you mark a class with #Service or #Compoenent and if spring's annotation scanning scope allows it to reach to the package, spring will register the instances of those classes as spring beans.
You can provide the packages to be included/excluded during scan with #ComponentScan
#Bean
#Beans are marked on factory methods which can create an instance of a particular class.
#Bean
public Account getAccount(){
return new DailyAccount();
}
Now in you application you can simply #Autowire Account and spring will internally call its factory method getAccount, which in turn returns an instance of DailyAccount.
There is a simple difference of using #Bean vs #Service or #Compoenent.
The first one makes your beans loosely coupled to each other.
In the #Bean, you have flexibility to change the account implementation without even changing any of the account classes.
Consider if your classes instantiation is a multi-step operation like read properties values etc then you can easily do it in your #Bean method.
#Bean also helps if you don't have source code access to the class you are trying to instantiate.
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
You need to opt-in to auto-configuration by adding the #EnableAutoConfiguration or #SpringBootApplication annotations to one of your #Configuration classes.
You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies. For simplicity, we often find that using #ComponentScan (to find your beans) and using #Autowired (to do constructor injection) works well.
One way is to define #Bean in the class annotated with
#SprinBootApplication
If you see #SprinBootApplication it is combination of many annotation, and one of them is #Configuration. So when you define #Bean in the Main class, it means it's inside #Configuration class.
According to Configuration docs :
Indicates that a class declares one or more #Bean methods and may be
processed by the Spring container to generate bean definitions and
service requests for those beans at runtime.
class annotated with #Configuration
When you define #Bean is a class annotated with #Configuration class, it means it is the part of spring configuration all the Beans define in it all available for Dependency-Injection.
I have also seen some code where neither of the 2 above approaches
have been used and yet dependency injection works fine. I have tried
to research a lot on this but could not find any concrete answer to
this. Is this possible?
I am assuming you are talking about Sterio-type annotation. Every sterio type annotation has #Component, according to docs :
Indicates that an annotated class is a "component". Such classes are
considered as candidates for auto-detection when using
annotation-based configuration and classpath scanning.

Spring framework #Configurable vs #Configuration

I seems have problem understanding these 2 annotation. I have try to read the javadocs but still cannot figure out. Can anyone help to explain with simple code about these 2 ?
Thank so much in advance.
You use #Configuration as a replacement to the XML based configuration for configuring spring beans. So instead of an xml file we write a class and annotate that with #Configuration and define the beans in it using #Bean annotation on the methods.
And finally you use AnnotationConfigApplicationContext to register this #Configuration class and thus spring manages the beans defined. Small example you can find at Spring Configuration Documentaion.
Quoting from the above link
It is just another way of configuration Indicates that a class declares
one or more #Bean methods and may be processed by the Spring container
to generate bean definitions and service requests for those beans at
runtime.
And #Configurable is an annotation that injects dependencies into objects that are not managed by Spring using aspectj libraries. i.e., you still use old way of instantiation with plain new operator to create objects but the spring will take care of injecting the dependencies into that object automatically for you.
#Configuration is the heart of the Java-based configuration mechanism and provides an alternative to XML-based configuration.
#Configuration classes are just like regular #Components classes, except that methods annotated with #Bean are used to factory beans.

Spring Boot : Disable AutoConfiguration of all modules

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.

Resources