一直提示這個bean沒有被定義 我明明定義了的哪里出錯了
package?com.autowiring; public?class?AutoService?{ ????private?AutoDao?aotudao1; public?AutoService(AutoDao?aotudao1){ System.out.println("AutoService構(gòu)造方法"); this.aotudao1?=?aotudao1; } public?void?setAotudao1(AutoDao?aotudao1)?{ System.out.println("AutoService?set方法"); this.aotudao1?=?aotudao1; } public?void?say(String?word)?{ ?????this.aotudao1.say(word); } }
package?com.autowiring; import?org.junit.Test; import?com.imooc.test.base.UnitTestBase; public?class?AutoTest?extends?UnitTestBase{ ????????public?AutoTest(){ ???????? super("classpath*:spring.autowiring.xml"); ????????} ?????????@Test ????????public?void?testbyname(){ ???????? AutoService?Service=super.getBean("auto"); ???????? Service.say("hello"); ????????} }
<?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans" ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ????xsi:schemaLocation="http://www.springframework.org/schema/beans ????????http://www.springframework.org/schema/beans/spring-beans.xsd" ??????default-autowire="byName"> ???????? <bean?id="aotudao1"?class="com.autowiring.AutoDao"??></bean> <bean?id="auto"?class="com.autowiring.AutoService"> </bean> ?</beans>
package?com.autowiring; public?class?AutoDao?{ ?????public?void?say(String?word){ ???? ?System.out.println("這是Dao層:"+word); ?????} }
錯誤信息:No bean named 'auto' is defined
我的配置文件哪里錯了
2017-12-13
沒有默認(rèn)的無參構(gòu)造方法,這個需要添加AutoService的無參構(gòu)造方法,不然值為byname或者byType時候AutoService的構(gòu)造函數(shù)會報錯吧
2017-06-11
<bean />
2017-05-22
把 main 方法也放出來吧
2017-05-22
在用?default-autowire的時候值為byname和bytype時 ?不能添加構(gòu)造方法 ? 在用constractor時可以用set方法
坑啊 ?這是為什么