问答详情
源自:4-2 Spring Bean装配之Autowired注解说明-1

spring中在私有变量和构造方法上使用@aware报错,在setter方法上就没有报错。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'injectionServiceImpl' defined in file [E:\myexlipseworkspace\Spring2\bin\injectionannotation\InjectionServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [injectionannotation.InjectionServiceImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: injectionannotation.InjectionServiceImpl.<init>()

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)

at org.spr


提问者:Justonedayzhupeng 2016-07-21 16:52

个回答

  • ucandoooh
    2017-12-02 20:13:04

    在私有变量上 或者 setter方法上@autowired,如果程序中还有自己自行撰写的构造函数,就会报错

    因为如果自行撰写了构造函数,编译程序就不会自动建立默认构造函数,所以会初始化失败

  • 慕工程2475207
    2017-06-15 16:01:56

    下图调用了该类的默认构造器,不会报错

    http://img.mukewang.com/59423e8d0001a11906760559.jpg

    下图调用了自己重写的构造器不会报错。

    http://img.mukewang.com/59423e8e00011ba506480557.jpg

    http://img.mukewang.com/59423e8f0001131607900563.jpg

    下图重写构造器没有使用注释,set方法无法去找到bean

    http://img.mukewang.com/59423e9100010ec807030564.jpg


  • LuckyBoy0129
    2016-07-21 20:10:00

    No default constructor found;

    setter设置注入就是调用setXXX方法,但是需要保留默认构造方法