1、InstantiationAwareBeanPostProcessor:实现InstantiationAwareBeanPostProcessor接口,重写postProcessBeforeInstantiation方法创建bean,
源码入口:createBean --> resolveBeforeInstantiation --> applyBeanPostProcessorsBeforeInstantiation
applyBeanPostProcessorsBeforeInstantiation这个方法有点小坑需要注意,如果你是多个InstantiationAwareBeanPostProcessor需要执行,当其中某一个
InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation返回结果不为空时,和面的InstantiationAwareBeanPostProcessor将不再执行
2、通过反射创建对象(spring创建bean的常规方式)
3、通过factoryMethod创建对象
4、通过FactoryBean创建对象
factoryMethod和FactoryBean从源码看走的是同一个接口instantiateUsingFactoryMethod,只是在配置上有所区别,举例说明:
class UserFactory { public User getUser(String username){ return new User(username); } } class UserStaticFactory { //这里是静态方法,使用factoryMethod方式,需要定义静态方法 public static User getUser(String username){ return new User(username); } }
5、通过Sulipper创建对象
源码入口:createBean --> doCreateBean --> createBeanInstance --> obtainFromSupplier