Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Spring Boot Application class needs to have @Configuration annotation?

I am learning about the Spring Framework but I can't understand what exactly the @Configuration annotation means and which classes should be annotated so. In the Spring Boot docs it is said that the Application class should be @Configuration class.

Spring Boot favors Java-based configuration. Although it is possible to call SpringApplication.run() with an XML source, we generally recommend that your primary source is a @Configuration class.

Trying to learn about @Configuration I find that annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions.

If that is so then how is this application class a source of bean definitions?

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class App  {     public static void main(String[] args) throws Exception {         SpringApplication.run(App.class, args);         } } 

I have pretty much understood most other basic concepts regarding Spring but I can't understand the purpose of @Configuration or which classes should be @Configuration classes? Can someone please help. Thanks !!

like image 441
varunkr Avatar asked Aug 31 '16 10:08

varunkr


People also ask

Why do we use @configuration annotation in spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

What does @configuration mean in spring boot?

@Configuration annotation 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. Since spring 2, we were writing our bean configurations to xml files.

What is the difference between @configuration and @EnableAutoConfiguration in spring boot?

How They Differ. The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.

How do @configuration annotated classes support Singleton beans?

If you use @Configuration, all methods marked as @Bean will be wrapped into a CGLIB wrapper which works as if it's the first call of this method, then the original method's body will be executed and the resulting object will be registered in the spring context.


2 Answers

You understood it right.

@Configuration

@Configuration is an analog for xml file. Such classes are sources of bean definitions by defining methods with the @Bean annotation.

@Configuration is:

  • not required, if you already pass the annotated class in the sources parameter when calling the SpringApplication.run() method;
  • required, when you don't pass the annotated class explicitly, but it's in the package that's specified in the @ComponentScan annotation of your main configuration class.

For readability, classes that are even explicitly passed as sources may anyway be annotated with @Configuration - just to show the intentions more clearly.

Your current class is not really source of bean definitions, because it doesn't have any, but if you had @Bean annotated methods, Spring would see them.

@EnableAutoConfiguration

Can be used with or without @Configuration. It tells Spring to setup some basic infrastructure judging by what you have in the classpath. It's done by invoking a so called import class that's derived from the value of the @Import annotation that @EnableAutoConfiguration includes. Only one class should be annotated with @EnableAutoConfiguration, duplicating it doesn't do anything.

This answer may also be helpful to understand the Spring Boot initialization process: Which piece of code in Spring Boot actually registers dispatcher servlet for SpringMVC?

like image 146
Artem Novikov Avatar answered Oct 06 '22 11:10

Artem Novikov


I think the main reason, why Spring Boot's @SpringBootApplication annotation automatically applies @Configuration is to allow to add bean definitions in the very same class. One of the main goals of Spring Boot is to allow you to create application fast and without extra movements. So by allowing you add bean definitions right into the Application's class, you don't need to create extra classes to hold your configuration. You have just 1 class and that's it.

like image 24
Nikem Avatar answered Oct 06 '22 11:10

Nikem