Solon Ioc 的魔法之注解注入器(也可叫虚空注入器)

很多人惊叹于 Solon 的注入能力,一个注解怎可注万物???

一、注解注入器

Solon Ioc 的四大魔法之一:注解注入器(BeanInjector<T extends Annotation>)。在扫描时,Solon 会检查相关组件的字段或者参数,上面有没有注解?如果有注解,有没有对应的注入器注册过?如果有,则执行注入器。

1、什么是注解?

注解一般也叫元数据,是一种代码级别的说明性内容。编译器在编译时,可以借助注解产生很多魔法效果;Solon Ioc 在运行时,也借助注解产生了很多魔法效果。

其中,注解注入器便是 Solon Ioc 的四大魔法之一。

2、注入器接口是怎么样的?

java 复制代码
@FunctionalInterface
public interface BeanInjector<T extends Annotation> {
    void doInject(VarHolder vh, T anno);
}

其中:

  • vh,用于接收变量数据
  • anno,则是申明的注解

3、Solon Ioc 的注入器注册接口

java 复制代码
void beanInjectorAdd(Class<T> annoClz, BeanInjector<T> injector);
void beanInjectorAdd(Class<T> annoClz, Class<?> targetClz, BeanInjector<T> injector);

二、为什么也可叫"虚空"注入器?

这个是因为,Solon 的注入是执行一个接口,而不是即定的内容。内容,可以是现成的,也可以是动态构建的。所以很"虚空"。

1、分解内置的的 @Inject 注解实现

@Inject 的简单使用示例

java 复制代码
//主入配置
@Component
public class DemoService{
    @Inject("${track.url}")
    String trackUrl;
    
    @Inject("${track.db1}")
    HikariDataSource trackDs;
}

//注入 bean
@Component
public class DemoService{
    @Inject
    private static TrackService trackService; 
    
    @Inject("userService")
    private UserService userService;
}

注入器的能力实现剖析(简单的示意实现,框架的实现比这个复杂)

java 复制代码
context.beanInjectorAdd(Inject.class, (vh, anno) -> {
    //申明:是否必须要注入?
    vh.required(anno.required());
    
    if (Utils.isEmpty(anno.value)) {
        //没有值,说明是 bean type 注入
        vh.content().getBeanAsync(vh.type(), bean->{ //vh.content() 即 context。在"热插拨"时可能会不同
            vh.setValue(bean);
        });
    } else {
        if(anno.value().startsWith("${")) {
            //说明是配置注入
            String val = vh.content().cfg().getByExpr(anno.value());
            vh.setValue(val);
        } else {
            //说明是 bean name 注入
            vh.content().getBeanAsync(anno.value(), bean->{
                vh.setValue(bean);
            });
        }
    }
});

2、"类型增强"注入器。魔法的升级!

Solon 内置的注入器,你不喜欢?

想换掉实现行不行?行!完全换掉代码太多,想为特定的类型增加注入行不行?也行!比如,我们设计了一个 EsMapper<T> 用于操作 Elasticsearch。然后可以自由的扩展:

java 复制代码
public interface AuthorMapper extends EsMapper<Author> {
}
public interface CommentMapper extends EsMapper<Comment> {
}
public interface ContactMapper extends EsMapper<Contact> {
}
public interface DocumentMapper extends EsMapper<Document> {
}

估计还会想扩展更多的子类?"类型增强" 注入器在手,一切我有。

java 复制代码
EsMapperFactory  esMapperFactory;

context.beanInjectorAdd(Inject.class, EsMapper.class, (vh, anno) -> {
    EsMapper mapper = esMapperFactory.create(vh.getType());
    vh.setValue(mapper);
});

可以再借用容器的"缓存"特性,同类型的注入性能就提高了:

java 复制代码
EsMapperFactory  esMapperFactory;

context.beanInjectorAdd(Inject.class, EsMapper.class, (vh, anno) -> {
    EsMapper mapper = vh.context().getBean(vh.getType());
    if (mapper == null) {
        mapper = esMapperFactory.create(vh.type());
        
        vh.context().wrapAndPut(mapper.getClass(), bean); //有可能被代理了,类型与 vh.getType() 不同
        vh.context().wrapAndPut(vh.getType(), bean);
    }
    vh.setValue(mapper);
});

如果有"多源"的概念,我们还可以支持 @Inject("name"):

java 复制代码
EsMapperFactory  esMapperFactory;

context.beanInjectorAdd(Inject.class, EsMapper.class, (vh, anno) -> {
    EsMapper mapper = null;
    if (Utils.isEmpty(anno.value)) {
        //按类型取
        mapper = vh.context().getBean(vh.getType());
    } else {
        //按名字取
        mapper = vh.context().getBean(anno.value());
    }
    
    if (mapper == null) {
        mapper = esMapperFactory.create(anno.value(), vh.type());
        
        if (Utils.isEmpty(anno.value)) {
            //按类注型注入;就按类型缓存
            vh.context().wrapAndPut(mapper.getClass(), bean); //有可能被代理了,类型与 vh.getType() 不同
            vh.context().wrapAndPut(vh.getType(), bean);
        } else {
            //按类名字注入;就按名字缓存
            vh.context().wrapAndPut(anno.value(), bean);
        }
    }
    vh.setValue(mapper);
});

现在我们可以用了(吃饭喽,下班喽!):

java 复制代码
//主入配置
@Component
public class DemoService{
    @Inject
    DocumentMapper documentMapper;
    
    @Inject("es2")
    DocumentMapper documentMapper2;
}
相关推荐
Joy T2 小时前
Agent 确定性上下文设计:从 Prompt 参数注入到能力分层
java·开发语言·微服务·架构·prompt·agent context
m0_587383005 小时前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
·云扬·5 小时前
Prompt 工程与安全:从 System 设计到注入防御
java·人工智能·安全·ai·prompt
代码不停6 小时前
自动化测试(java)
java·开发语言·selenium·自动化
鬼手点金8 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
paopaokaka_luck9 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
Wang's Blog9 小时前
Java 项目实战: 交友项目 - Dubbo 的内部结构与一次调用的完整链路
java·dubbo
Wang's Blog9 小时前
Java 项目实战: 交友项目 - Dubbo 入门引导
java·dubbo·交友
蜗牛互联网10 小时前
Python Agent工具调用的白名单、参数校验与审批门禁
java·服务器·前端·人工智能·后端·python
SL_staff10 小时前
当规则引擎开始‘懂业务’:JVS-Rules金融风控四大原生能力拆解
java·python·全栈