public class BeanServiceImpl implements BeanService { }
@Configuration public class ConfigurationBean { @Bean public BeanService beanService() { BeanServiceImpl beanService = new BeanServiceImpl(); System.out.println("beanService: " + beanService); return beanService; } @Bean //@Bean 如果不知道name,默认bean的name为方法名,下面等同于@Bean("beanService1") public BeanService beanService1() { BeanServiceImpl beanService1 = new BeanServiceImpl(); System.out.println("beanService1: " + beanService1); return beanService1; } }
@Component public class SetterBean { private BeanService bs; public SetterBean(BeanService beanService) { bs = beanService; System.out.println("有参构造:" + bs); } }
绝大部分是通过反射构造器去创建,详情源码见
AbstractAutowireCapableBeanFactory.doCreateBean
中的createBeanInstance
方法
如果没有有参构造器,默认使用无参的构造方法反射创建实例,详情见
createBeanInstance – instantiateBean
方法
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { ... ... // No special handling: simply use no-arg constructor. return instantiateBean(beanName, mbd); }
如果有唯一有参构造器,并且参数能在Spring容器里找到,则通过该有参构造器反射创建
详情见createBeanInstance – autowireConstructor
方法
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { ... ... // Candidate constructors for autowiring&#63; Constructor<&#63;>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } ... ... }
如果参数不能在Spring容器中找到,就会报
NoUniqueBeanDefinitionException
异常
有多个有参构造器,Spring在创建Bean实例时,就不知道用哪个构造器去创建,就会报如下异常:
&#8195;&#8195;BeanInstantiationException: Failed to instantiate [XXX]: No default constructor found;
@Component public class SetterBean { private BeanService bs; public SetterBean(BeanService beanService) { bs = beanService; System.out.println("有参构造:" + bs); } //@Autowired public SetterBean(BeanService beanService, BeanService beanService1) { bs = beanService; System.out.println("有参构造:" + bs + "--------" + beanService1); } }
可以通过
像3.2 中,把@Autowired注释打开,就会使用该有参构造创建实例,启动就不会报异常,@Autowired
来指定有参构造来创建实例
这个方法主要是挑选构造器,然后创建实例的
到此这篇关于Spring Bean实例的创建及构造器的挑选的文章就介绍到这了,更多相关Bean实例创建及构造器挑选内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!