Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
218 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...