亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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


正在回答

3 回答

在私有變量上 或者 setter方法上@autowired,如果程序中還有自己自行撰寫的構造函數,就會報錯

因為如果自行撰寫了構造函數,編譯程序就不會自動建立默認構造函數,所以會初始化失敗

0 回復 有任何疑惑可以回復我~

下圖調用了該類的默認構造器,不會報錯

http://img1.sycdn.imooc.com//59423e8d0001a11906760559.jpg

下圖調用了自己重寫的構造器不會報錯。

http://img1.sycdn.imooc.com//59423e8e00011ba506480557.jpg

http://img1.sycdn.imooc.com//59423e8f0001131607900563.jpg

下圖重寫構造器沒有使用注釋,set方法無法去找到bean

http://img1.sycdn.imooc.com//59423e9100010ec807030564.jpg


1 回復 有任何疑惑可以回復我~

No default constructor found;

setter設置注入就是調用setXXX方法,但是需要保留默認構造方法

1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

spring中在私有變量和構造方法上使用@aware報錯,在setter方法上就沒有報錯。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號