Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use spring.factories for Spring Boot auto-configuration instead of annotations?

Tags:

spring-boot

The documentation states:

Developing auto-configuration and using conditions

If you work in a company that develops shared libraries, or if you work on an open-source or commercial library, you might want to develop your own auto-configuration. Auto-configuration classes can be bundled in external jars and still be picked-up by Spring Boot.

If I have annotations for everything else (even @AutoConfigureAfter or @AutoConfigureBefore annotations),

Why maintain a properties file to point to a class with an annotation?

like image 995
Chuck C Avatar asked Aug 20 '15 00:08

Chuck C


People also ask

What is a benefit of the auto configuration preset in spring boot?

Simply put, the Spring Boot auto-configuration helps us automatically configure a Spring application based on the dependencies that are present on the classpath. This can make development faster and easier by eliminating the need to define certain beans included in the auto-configuration classes.

What is the purpose of spring factories?

spring. factories file, located in META-INF/spring. factories location on the classpath, is used by Auto Configuration mechanism to locate Auto Configuration Classes. Each module that provides Auto Configuration Class needs to have METAINF/spring.

What is the difference between spring boot application and enable auto configuration annotation?

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.

What is the use of spring boot auto configuration?

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then we will auto-configure an in-memory database.


1 Answers

Because we are not going to scan the world to figure out what auto-configuration classes exist in your project. For one, an auto-configuration is just a regular @Configuration class.

The way a Spring component is found is via explicit declaration or component scan but we need to know the list of auto-configuration classes way before we actually start the context.

like image 79
Stephane Nicoll Avatar answered Oct 25 '22 03:10

Stephane Nicoll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!