首页 > 其他分享 >Solon2 开发之容器,三、注入或手动获取 Bean

Solon2 开发之容器,三、注入或手动获取 Bean

时间:2023-02-12 22:01:02浏览次数:33  
标签:容器 bean 获取 Bean context Solon2 userService public

1、如何注入Bean?

先了解一下Bean生命周期的简化版:

  1. 运行构建函数
  2. 尝试字段注入(有时同步注入,没时订阅注入。不会有相互依赖而卡住的问题)
  3. @Init 函数(是在容器初始化完成后才执行)
  4. ...
  5. 释放(基本不会发生)

了解这个生命过程后,可以知道注入的内容,在构建函数里是不可用的:

@Service
public class DemoService{
    
    //通过bean type注入(注入是异步的,不能在构造函数里使用)
    @Inject
    private TrackService trackService;
    
    //通过bean name注入
    @Inject("userService")
    private UserService userService;
    
    public DemoService(){
       //注入的Bean,不能在构造函数里使用。在生命周期时,构建先于注入
    }
    
    @Init
    public void init(){
        //注入的Bean,需要进一步做初始化,请在处理
    }
}

引用已有 Bean 构建新的 Bean:

@Configuration
public class DemoConfig{
    //提示:@Bean 只能与 @Configuration 配合
    @Bean("ds3") 
    public DataSource init(@Inject("ds1") DataSource ds1, @Inject("ds2") DataSource ds2){
        Map<String, DataSource> dsMap = new HashMap<>();
        dsMap.put("ds1", ds1);
        dsMap.put("ds2", ds2);
        
        DynamicDataSource tmp = new DynamicDataSource();
        tmp.setStrict(true);
        tmp.setTargetDataSources(dsMap);
        tmp.setDefaultTargetDataSource(ds1);
        
        return tmp;
    }
}

2、如何手动获取Bean?

  • 同步获取
public class DemoService{
    private TrackService trackService;
    private UserService userService;
    
    public DemoService(){
        //同步方式,根据bean type获取Bean(如果此时不存在,则返回null。需要注意时机)
        trackService = Solon.context().getBean(TrackService.class);
        
        //同步方式,根据bean type获取Bean(如果此时不存在,自动生成一个Bean并注册+返回)
        trackService = Solon.context().getBeanOrNew(TrackService.class);
       
        //同步方式,根据bean name获取Bean(如果此时不存在,则返回null)
        userService = Solon.context().getBean("userService");
    }
}
  • 异步获取(如果存在,会直接回调;如果没有,目标产生时会通知回调)
public class DemoService{
    private TrackService trackService;
    private UserService userService;
    
    public DemoService(){
        //异步订阅方式,根据bean type获取Bean(已存在或产生时,会通知回调;否则,一直不回调)
        Solon.context().getBeanAsync(TrackService.class, bean-> {
            trackService = bean;
            
            //bean 获取后,可以做些后续处理。。。
        });
        
        //异步订阅方式,根据bean name获取Bean
        Solon.context().getBeanAsync("userService", bean-> {
            userService = bean;
        });
    }
}

注入模式是必须要被扫描到的。有时候不方便扫描,或者不必扫描,那手动模式就是很大的一种自由。

3、获取一批有特征的Bean?

  • 通过订阅接口
context.subBeansOfType(DataSource, bean->{
    //获取所有 DataSource Bean
    //一般由:@Component 产生 或者 @Configuration + @Bean 产生
});

context.subWarpsOfType(DataSource, bw->{
    // bw.name() 获取 bean name 
    // bw.get() 获取 bean
    //一般由:@Component 产生 或者 @Configuration + @Bean 产生
});
  • 通过容器加载完成事件 beanOnloaded ,遍历已注册的 Bean
//在 beanOnloaded 事件进进行遍历,确保所有 Bean 已处理完成

//a. 获取 name "share:" 开头的 bean  //context:AopContext
context.beanOnloaded((ctx) -> {
    ctx.beanForeach((k, v) -> {
        if (k.startsWith("share:")) {
            render.putVariable(k.split(":")[1], v.raw());
        }
    });
});

//b. 获取 IJob 类型的 bean  //context:AopContext
context.beanOnloaded((ctx) -> {
    ctx.beanForeach((v) -> {
        if (v.raw() instanceof IJob) {
            JobManager.register(new JobEntity(v.name(), v.raw()));
        }
    });
});

标签:容器,bean,获取,Bean,context,Solon2,userService,public
From: https://www.cnblogs.com/noear/p/17114818.html

相关文章

  • Solon2 开发之容器,四、注入依赖与初始化
    Solon强调有克制的注入+手动控制结合的模式。好处是,代码用料少、启动快。Bean的关键生命节点:节点说明1.Constructor(构造方法)不支持参数注入2.@In......
  • Solon2 开发之容器,一、注入或手动获取配置
    约定resources/app.yml(或app.properties)#为应用配置文件配置样例track:name:xxxurl:http://a.a.adb1:jdbcUrl:"jdbc:mysql://..."username:......
  • Solon2 开发之容器,二、构建一个 Bean 的三种方式
    1、手动简单的构建://生成普通的BeanSolon.context().wrapAndPut(UserService.class,newUserServiceImpl());//生成带注解的Bean(比如:@Controller)Solon.context().be......
  • Bean的拓展和应用
    一.Bean的作用域1.单例模式(spring的默认机制,即拿相同的bean的时候对象都是相同的,不会造成资源浪费)<beanid="user"class="top.lostyou.pojo.user"p:name="jay周"p:se......
  • 容器部署分布式zabbix
    之前有写过docker-compose部署zabbix的博客这里再总结下分布式部署zabbix的笔记,这里重点是部署zabbix-proxy同样需要准备数据库配置文件数据库配置文件差不多这里是doc......
  • Docker consul的容器服务更新与发现
    一、Consul概述(1)什么是服务注册与发现服务注册与发现是微服务架构中不可或缺的重要组件。起初服务都是单节点的,不保障高可用性,也不考虑服务的压力承载,服务之间调用单纯的......
  • 022_第三方 bean 属性绑定
    @ConfigurationProperties使用 @ConfigurationProperties为第三方bean绑定属性:    解除使用@ConfigurationProperties注释警告:    @EnableConf......
  • #yyds干货盘点# LeetCode面试题:盛最多水的容器
    1.简述:给定一个长度为n的整数数组 height 。有 n 条垂线,第i条线的两个端点是 (i,0) 和 (i,height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可......
  • 【Docker】MySQL容器定时备份
    我们通常使用原生的mysql会比较多,mysql的备份也耳熟能详。假如现在有个mysql数据库username为root,password为123456,且现在要导出schema为db1、db2的数据。在本地导出的时候......
  • div、td 、p 等容器内强制换行和不换行的方法
    转载自:http://www.divcss5.com/html/h50327.shtml1、强制不换行,同时以省略号结尾。<divstyle="width:100px;overflow:hidden;white-space:nowrap;text-overflow:ellipsi......