Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of Configuration in SpringBoot

I am trying to understand how beans that we make using @Configuration tends to override the beans that are generated by SpringBoot by default. I have been working on a project where in many cases we create beans for things like ZuulConfigs and the assumption is, whatever we are making shall take precedence over the default generated bean. I have been trying to figure this out but can't. Basically,

  1. Is Spring achieving this via some custom class loader
  2. If not how is this precedence working. Can I give some precedence in similar manner to my beans
  3. Can I generate similar hierarchy in my project,if so how

The help is highly appreciated

like image 383
chinmay Avatar asked Jun 01 '17 18:06

chinmay


People also ask

In what order the properties are loaded in Spring boot?

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values, properties are considered in the the following order: Command line arguments. JNDI attributes from java:comp/env . Java System properties ( System.

How is Spring boot configuration done?

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.

What is the order of bean creation in Spring?

The order in which Spring container loads beans cannot be predicted. There's no specific ordering logic specification given by Spring framework. But Spring guarantees if a bean A has dependency of B (e.g. bean A has an instance variable @Autowired B b; ) then B will be initialized first.

What is the use of order in Spring boot?

The @Order annotation defines the sorting order of an annotated component or bean. It has an optional value argument which determines the order of the component; the default value is Ordered. LOWEST_PRECEDENCE. This marks that the component has the lowest priority among all other ordered components.


2 Answers

Spring AutoConfiguration is used to provide a basic configuration if certain classes are in the classpath or not.

If you want to configure the order in which beans are instantiated by spring you can use

@DependsOn("A") 
 public class B {
 ...    
}

This would create bean "A", then "B". Hence you can order the configuration depending upon the beans need first to be done. Anyways Spring automatically detects the dependencies by analyzing the bean classes. for more help check this question Spring Boot AutoConfiguration Order

Alternative : There is also "@AutoConfigureOrder" annotation(where you can prioritise the configuration), you can have a look in the code for deeper understanding.

Documentation of AutoConfiguration is here

like image 166
virsha Avatar answered Oct 02 '22 13:10

virsha


First of all, class loading and bean creation are two different things. We don't need to create a bean to load a class, however, a class has to be loaded in order to create a bean.

Now, coming back to Spring's example, Spring looks into all the packages configured by @componentScan and creates beans of all the classes annotated with @Bean, @Configuration and/or @Component. Spring's container keeps track of all the beans created and hence, when it encounters user defined bean with same name and class type as default bean, it replaces the original definition with user defined one (e.g. we can create our custom @ObjectMapper to override Spring boot's own instance). You can also use @Primary annotation to make you bean take precedence if another definition with same class exists (documentation here).

Below are the answers for your questions:

  1. Spring uses reflection to load the classes and create instances. Although you can load the classes with your custom class loader (more on that here), you don't need to worry about it for @Configuration.
  2. Yes, you can use @Primary annotation to give your bean a precedence. You can also use @Order(here) to define the creation order for your beans.
  3. With @Primary, @Order and @Qualifier annotation you can define your own hierarchy for bean creation.
like image 29
Darshan Mehta Avatar answered Oct 02 '22 11:10

Darshan Mehta