当前位置: 代码网 > it编程>编程语言>Java > Springboot加载所有Bean之后运行方式

Springboot加载所有Bean之后运行方式

2024年07月18日 Java 我要评论
springboot加载所有bean之后运行方式springboot启动后,需要加载一些配置文件至内存中方法编写普通类,继承applicationlistener,重写onapplicationeve

springboot加载所有bean之后运行方式

springboot启动后,需要加载一些配置文件至内存中

方法

编写普通类,继承applicationlistener,重写onapplicationevent方法

@component
@slf4j
public class inittasklistener implements applicationlistener<contextrefreshedevent> {

@sneakythrows
    @override
    public void onapplicationevent(contextrefreshedevent event) {
        system.out.println("所有bean加载完之后开始执行....");
    }

}

springboot中bean的加载顺序

一、为什么要控制

当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成自动装配时。

你会声明自己的configuration类,但是可能你面对的是好几个有互相依赖的bean。

如果不加以控制,这时候可能会报找不到依赖的错误,这个时候需要通过一些手段来控制springboot中的bean加载顺序。

二、怎么控制

@dependson

@dependson注解可以用来控制bean的创建顺序,该注解用于声明当前bean依赖于另外一个bean。所依赖的bean会被容器确保在当前bean实例化之前被实例化。

与@component或@bean配合使用 

demo

@slf4j
@configuration
@configurationproperties(prefix = "dict")
public class springconfig {
    @component(value = "eventsource")
    public class eventsource {
        public eventsource(){
            system.out.println("事件源创建");
        }
    }
    /**
     * 监听类
     */
    @component
    @dependson(value = {"eventsource"})
    public class eventtlistener {

        public eventtlistener(){
            system.out.println("监听器创建");
        }
    }
}

参数注入

package com.sinosoft.springbootplus.test.config;

import lombok.extern.slf4j.slf4j;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.dependson;
import org.springframework.stereotype.component;

/**
 * @author lsh
 * @date 2022/2/25
 */
@slf4j
@configuration
@configurationproperties(prefix = "dict")
public class springconfig {
    @component
    public class event{
        public event(){
            system.out.println("事件事件");
        }
    }
    
    @component
    public class eventsource{
        public eventsource(event e){
            system.out.println("事件源创建");
        }
    }


    @component
    public class eventtlistener {

        public eventtlistener(){
            system.out.println("监听器创建");
        }
    }
}

利用bean的生命周期中的扩展点

@autoconfigureorder

@autoconfigureorder只能改变外部依赖的@configuration的顺序。

这是不对的用法

@slf4j
@configuration
@configurationproperties(prefix = "dict")
public class springconfig {
    @component
    @autoconfigureorder(1)
    public class event{
        public event(){
            system.out.println("事件事件");
        }
    }

    @component
    @autoconfigureorder(2)
    public class eventsource{
        public eventsource(event e){
            system.out.println("事件源创建");
        }
    }


    @component
    @autoconfigureorder(3)
    public class eventtlistener {

        public eventtlistener(){
            system.out.println("监听器创建");
        }
    }
}

以上内容发现,在config里配置是不起作用的。

这是正确的用法

创建两个配置类

@slf4j
@configuration
@autoconfigureorder(1)
public class springconfig {
    @component
    public class event{
        public event(){
            system.out.println("首先在springconfig");
        }
    }

}


@slf4j
@configuration
@autoconfigureorder(2)
public class newconfig {
    @component
    public class event{
        public event(){
            system.out.println("然后在newconfig");
        }
    }
}

测试

发现结果是不正确的,注解还是没有生效。

当前工程里增加配置 meta-inf/spring.factories,内容为项目中的配置类

org.springframework.boot.autoconfigure.enableautoconfiguration=com.sinosoft.springbootplus.common.config.newconfig,com.sinosoft.springbootplus.common.config.springconfig

测试结果如图(正确)

三、遇到的问题

1、需要根据配置决定生成哪个实现类。

当在配置文件中配置的dict.cachetype的值是local时,初始化localisysdictrepository交给spring容器管理;当项目依赖了redis并且配置文件中配置的dict.cachetype的值是redis时,初始化redisisysdictrepository交给spring容器管理。

2、但是我又在这两个实现类上加了@repository注解,也要交给spring管理,这个时候项目启动就报错了。(通俗的来说一个类只能一次交给spring管理)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持3w代码。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com