1、剖析 Bean 的装包与注册过程
比如,用配置器装配一个 Bean (本质是装配出一个 BeanWrap,并自动注册到容器):
@Configuration
public class Config{
//同时以名字和类型进行注册 //支持类型或名字注入
@Bean(name="demo", typed=true)
public DemoService demo(){
return new DemoServiceImpl();
}
}
以上代码。转成全手动操控的完整过程如下(内部差不多就这么处理,不要把它用于日常开发):
DemoService bean = new DemoServiceImpl();
BeanWrap bw = new BeanWrap(Solon.context(), DemoServiceImpl.class, bean, "demo", true);
//实现上面配置器的效果,需要四行代码
Solon.context().putWrap("demo", bw);
Solon.context().putWrap("org.demo.DemoServiceImpl", bw);
Solon.context().putWrap(DemoServiceImpl.class, bw);
Solon.context().putWrap(DemoService.class, bw);
//如果是泛型还会有:
Solon.context().putWrap("org.demo.DemoServiceImpl<D>", bw);
Solon.context().putWrap("org.demo.DemoService<T,Y>", bw);
以上注册,我们可以这样获取 BeanWrap:
//这是常用的获取方式
Solon.context().getWrap("demo");
Solon.context().getWrap(DemoService.class);
//也可以
Solon.context().getWrap("org.demo.DemoServiceImpl");
Solon.context().getWrap(DemoServiceImpl.class);
2、Bean 在容器里是有两层信息
从上面的过程,可以得出 Bean 在容器里是有两层信息:
- 自身包装器的元信息
- 在容器里的注册信息(一个包装,可以有多条注册记录)
文章详细剖析了Bean如何通过配置器装配成BeanWrap并注册到容器中,具体展示了手动完成这一过程所需的步骤,包括创建Bean实例、创建BeanWrap、以及在Solon.context中添加多条注册记录。同时,指出Bean在容器中有两层信息,即自身包装器的元信息和注册信息。

1363

被折叠的 条评论
为什么被折叠?



