InstantiationStrategy

spring中反射实例化规范

InstantiationStrategy源码

无参构造方法实例化

默认情况下的反射实例化bean方式。

Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner)

指定构造方法实例化

指定 或 选择最合适的构造器,完成bean的实例化。

Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, Constructor<?> ctor, Object... args)

工厂方法实例化

xml文件中bean的factory-method属性,实际实例化bean方式。

Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, @Nullable Object factoryBean, Method factoryMethod, Object... args)

SimpleInstantiationStrategy实现类

实现了InstantiationStrategy接口,完成三个方法对应实现,扩展设计两个接口。

实例化 无参数构造器 的代理对象

protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
	throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");
}

实例化 有参数构造器 的代理对象

protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, @Nullable Constructor<?> ctor, Object... args) {
    throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");
}

CglibSubclassingInstantiationStrategy实现类

继承SimpleInstantiationStrategy,完成代理对象 有参数和无参数 创建方法。

源码

此实现类中包含两个很有趣的内部类:ReplaceOverrideMethodInterceptor和LookupOverrideMethodInterceptor,分别对应spring中实例化bean方式中的 replace-method和lookup-method方法。

总结

spring中的反射创建对象、replace-method、lookup-method、factory-method创建对象,本质上都是通过实例化策略InstantiationStrategy完成对象的实例化。

有兴趣可以在具体实现流程。<感觉没必要继续看下去,就是反射创建对象>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值