Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the priority of SpringBoot Bean loading

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to understand the problem of SpringBoot Bean loading priority". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problems with Bean load priority

The order in which spring containers load bean is uncertain, and the spring framework does not agree on specific sequential logic specifications. But spring guarantees that if A depends on B (such as the variable @ Autowired B in beanA), then B will be loaded before A.

Loading order in the same class

Constructor > > @ Autowired > > @ PostConstruct > > @ Bean

@ DependsOn control order

If An is not dependent on B, but A needs to be initialized after B, you can use @ DependsOn (value= "Bbeanname"). You need to specify Name manually above @ Bean of B, otherwise you won't find it.

@ Order cannot control the order

The @ Order annotation does not change the Bean load priority, and the @ Order annotation is used to set the order in which the Bean is loaded into the list

@ Order (2) @ Componentpublic class AnoBean1 implements IBean {private String name = "ano order bean 1"; public AnoBean1 () {System.out.println (name);}} @ Order (1) @ Componentpublic class AnoBean2 implements IBean {private String name = "ano order bean 2"; public AnoBean2 () {System.out.println (name) } @ Componentpublic class AnoTestBean {public AnoTestBean (List anoBeanList) {for (IBean bean: anoBeanList) {System.out.println ("in ano testBean:" + bean.getClass (). GetName ());}

The above code outputs the result

Ano order bean 1

Ano order bean 2

In ano testBean: AnoBean2

In ano testBean: AnoBean1

Spring controls Bean loading order using Spring @ Order to control bean loading order

Two demo bean

Package com.ziyear.spring4_2.order;public class Demo1Service {} package com.ziyear.spring4_2.order;public class Demo2Service {}

Two configuration classes, note the order in which the @ Order configuration is loaded

Package com.ziyear.spring4_2.order;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;@Configuration@Order (2) public class Demo1Config {@ Bean public Demo1Service demo1Service () {System.out.println ("demo1config loaded"); return new Demo1Service ();} package com.ziyear.spring4_2.order;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration Import org.springframework.core.annotation.Order;@Configuration@Order (1) public class Demo2Config {@ Bean public Demo2Service demo2Service () {System.out.println ("demo2config loaded"); return new Demo2Service ();}

Running

Package com.ziyear.spring4_2.order;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main (String [] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ("com.ziyear.spring4_2.order");}}

Output result

Demo2config loaded

Demo1config loaded

Use Spring @ DependsOn to control the bean loading order

The order in which spring containers load bean is uncertain, and the spring framework does not agree on specific sequential logic specifications. But spring guarantees that if A depends on B (such as the variable @ Autowired B in beanA), then B will be loaded before A. But if beanA doesn't rely on B directly, how do we get B to load first?

Control the initialization order of bean

There may be scenarios where bean An indirectly depends on bean B. For example, Bean B should need to update some global cache, possibly through singleton mode and not registered in the spring container, and bean A needs to use that cache; therefore, if bean B is not ready, bean A cannot access it.

In another scenario, bean An is the event publisher (or JMS publisher), and bean B (or some) is responsible for listening for these events, typically in Observer mode. We don't want B to miss any events, so B needs to be initialized first.

In short, there are many scenarios where bean B should be initialized before bean A to avoid all kinds of negative effects. We can use the @ DependsOn annotation on bean A to tell the container that bean B should be initialized first. The following is illustrated by an example.

Example illustration

The example illustrates Person and Man through the event mechanism, and then runs through the spring configuration. The example is simplified for ease of illustration.

Person class

Public class Person {public static void say () {System.out.println ("person.say (): Im a person");}}

Man class

Public class Man {public void say () {System.out.println ("man.say (): Im a man:");}}

AppConfig.java

Configure the running class.

@ Configuration@ComponentScan ("com.ziyear.demo") public class Appconfig {@ Bean (initMethod = "say") @ DependsOn ("man") public Person personBean () {return new Person ();} @ Bean (name = "man", initMethod = "say") / / @ Lazy public Man manBean () {return new Man ();} public static void main (String [] strings) {new AnnotationConfigApplicationContext (Appconfig.class) }}

Run the main method of AppConfig, and the output is:

Man.say (): Im a man:

Person.say (): Im a person

That's all for the content of "how to understand the SpringBoot Bean loading priority". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report