Spring控制Bean加载顺序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38405770/article/details/86654500

(1)使用Spring @Order控制bean加载顺序

  • 两个演示bean
package com.ziyear.spring4_2.order;

public class Demo1Service {

}
package com.ziyear.spring4_2.order;

public class Demo2Service {

}
  • 两个配置类,注意@Order配置加载的顺序
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 加载了");
        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 加载了");
        return new Demo2Service();
    }

}
  • 运行
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");
    }


}

输出结果

demo2config 加载了
demo1config 加载了

(2)使用Spring @DependsOn控制bean加载顺序

spring容器载入bean顺序是不确定的,spring框架没有约定特定顺序逻辑规范。但spring保证如果A依赖B(如beanA中有@Autowired B的变量),那么B将先于A被加载。但如果beanA不直接依赖B,我们如何让B仍先加载呢?

控制bean初始化顺序
可能有些场景中,bean A 间接依赖 bean B。如Bean B应该需要更新一些全局缓存,可能通过单例模式实现且没有在spring容器注册,bean A需要使用该缓存;因此,如果bean B没有准备好,bean A无法访问。

另一个场景中,bean A是事件发布者(或JMS发布者),bean B (或一些) 负责监听这些事件,典型的如观察者模式。我们不想B 错过任何事件,那么B需要首先被初始化。

简言之,有很多场景需要bean B应该被先于bean A被初始化,从而避免各种负面影响。我们可以在bean A上使用@DependsOn注解,告诉容器bean B应该先被初始化。下面通过示例来说明。

示例说明
示例通过事件机制说明,Person和Man,然后通过spring配置运行。为了方便说明,示例进行了简化。

Person类

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

Man类

public class Man {

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

AppConfig.java
配置运行类。

@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);
    }

}

运行AppConfig的main方法,输出结果为:

man.say():Im a man:
person.say():Im a person

总结
如果我们注释掉@DependsOn(“man”),我们可能不确定获得相同结果。尝试多次运行main方法,偶尔会看到不同的say方法别执行。为什么是偶尔呢?因为容器启动过程中,spring按任意顺序加载bean。

那么当不使用@DependsOn可以让其100%确定吗?可以使用@Lazy注解放在manBean ()上。因为Man在启动阶段不加载,当其他bean需要其时才加载。这次我们仅Person被初始化。

person.say():Im a person

现在从新增加@DependsOn,也不删除@Lazy注解,输出结果和第一次一致,虽然我们使用了@Lazy注解,Man在启动时仍然被加载,因为@DependsOn表明需要Man。

猜你喜欢

转载自blog.csdn.net/weixin_38405770/article/details/86654500