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

一、注解注入器

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

1、什么是注解?

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

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

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

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

其中:

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

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

void beanInjectorAdd(Class<T> annoClz, BeanInjector<T> injector);
void beanInjectorAdd(Class<T> annoClz, Class<?> targetClz, BeanInjector<T> injector);

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

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

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

@Inject 的简单使用示例

//主入配置
@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;
}

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

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。然后可以自由的扩展:

public interface AuthorMapper extends EsMapper<Author> {
}
public interface CommentMapper extends EsMapper<Comment> {
}
public interface ContactMapper extends EsMapper<Contact> {
}
public interface DocumentMapper extends EsMapper<Document> {
}

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

EsMapperFactory  esMapperFactory;

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

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

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")

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

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

//主入配置
@Component
public class DemoService{
@Inject
DocumentMapper documentMapper; @Inject("es2")
DocumentMapper documentMapper2;
}

Solon Ioc 的魔法之注解注入器(也可叫虚空注入器)的更多相关文章

  1. Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  2. Solon Ioc 的注解对比Spring及JSR330

    注解对比 Solon 1.0.10 Spring JSR 330 @XInject * @Autowired @Inject 字段或参数注入 @XBean * @Component @Named Be ...

  3. Springboot mini - Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  4. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  5. 阶段3 2.Spring_05.基于XML的IOC的案例1_4 注解IOC案例-把自己编写的类使用注解配置

    注解改造案例 复制之前的xml配置的pom.xml里面的依赖. 复制com文件 bean.xml配置文件也拷贝过来 测试类也复制过来 开始基于注解的IOC配置 右键项目,选择maven.选择更新 更新 ...

  6. Solon 的 PathVariable 不需注解

    相对于 Spring boot 的 path variable :Solon 的不需注解,只要变量名对上即可: //path var demo // @XMapping("e/{p_q}/{ ...

  7. 死磕Spring之IoC篇 - @Autowired 等注解的实现原理

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  8. 死磕Spring之IoC篇 - @Bean 等注解的实现原理

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  9. IoC容器-Bean管理注解方式(创建对象)

    IoC操作Bean管理(基于注解方式) 1,什么是注解 (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...) (2)使用注解,注解作用在类上面,方法上面,属性上面 ( ...

  10. Spring(IOC自动装配-基于注解开发)

    Spring IoC 自动装载 autowire: 自动装载是Spring提供的一种更加简单的方式,来完成DI,不需要手动配置property ,IoC容器会自动选择Bean玩成注入. 自动装载俩种: ...

随机推荐

  1. tensorflow的官方强化学习库agents的相关内容及一些注意事项

    源代码地址: https://github.com/tensorflow/agents TensorFlow给出的官方文档说明: https://tensorflow.google.cn/agents ...

  2. Git 使用中的一些例子

    本文紧接前文: .gitignore文件的使用方法(学习总结版)-- .gitignore 文件的配合用法 ============================================= ...

  3. AI 大模型时代呼唤新一代基础设施,DataOps 2.0和调度编排愈发重要

    在 AI 时代,DataOps 2.0 代表了一种全新的数据管理和操作模式,通过自动化数据管道.实时数据处理和跨团队协作,DataOps 2.0 能够加速数据分析和决策过程.它融合了人工智能和机器学习 ...

  4. blender-十大基本操作

  5. 07-canvas绘制虚线

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  6. Java抽象类 小白版

    什么是抽象 抽象就是从多个事物中将共性的,本质的内容抽象出来. 什么是抽象类 Java语言中,用abstract关键字修饰的类叫作抽象类.类本身是不存在的,所以抽象类无法创建对象无法实例化. 在面向对 ...

  7. 【CMake系列】07-export与find

    为了将我们的库文件更方便地提供给他人使用,cmake 提供了一种方式,通过查找 .cmake 文件,将库导入项目中. 本节学习的内容,就是将我们的库导出一个 xxx.cmake 文件,以及 在项目中导 ...

  8. 【CMake系列】05-静态库与动态库编译

    在各种项目类型中,可能我们的项目就是一个 库 项目,向其他人提供 我们开发好的 库 (windows下的 dll /lib : linux下的 .a / .so):有时候在一个项目中,我们对部分功能 ...

  9. NetCore消息管道 Middleware

    中间件定义 /// <summary> /// 自定义中间件1 /// </summary> public class MyMiddleware : IMiddleware { ...

  10. Uncaught SyntaxError: The requested module ‘/node_modules/.vite/vue-router.js?v=2f0f3daf’ does not provide an export named ‘VueRouter’

    Uncaught SyntaxError: The requested module '/node_modules/.vite/vue-router.js?v=2f0f3daf' does not p ...