在進行 SSI整合時,總是報下邊的錯誤: nested exception is java.lang.IllegalArgumentException: Property 'sqlMapClient' is required
詳細的錯誤信息如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlMapClient' is required
在配置文件applicationContext中查看后發現配置中沒有properties標簽,很明顯是沒有將sqlMapClien注入到personDao中:
<bean id="personDao" class="com.zaj.dao.PersonDao"/>
正常思維就是將已經注冊的sqlMapClient注入到personDao中,修改如下:
<bean id="personDao" class="com.zaj.dao.PersonDao">
<property name="sqlMapClient" ref="sqlMapClient"></property> </bean>
試運行,沒錯,運行正確,錯誤沒了。可是我對照事例中的xml文件,明明是沒有這個properties的,這到底是為什么?仔細對比,發現在事例的xml文件頭里邊,有一個屬性,如下:
default-autowire="byName"
按着事例的樣子,把屬性加上,注釋掉剛才新加的properties,再次運行,果然可以,看到byName,想起來原來好像聽馬哥(馬士兵)說過,有byName和byType自動加載的屬性,基於學習起見,找出spring的參考文檔,找啊找,終於找到下邊的內容:
Optional attribute controlling whether to "autowire" bean properties. This is an automagical process in which bean references don't need to be coded explicitly in the XML bean definition file, but Spring works out dependencies. There are 5 modes: 1. "no" The traditional Spring default. No automagical wiring. Bean references must be defined in the XML file via the <ref> element. We recommend this in most cases as it makes documentation more explicit. 2. "byName" Autowiring by property name. If a bean of class Cat exposes a dog property, Spring will try to set this to the value of the bean "dog" in the current factory. If there is no matching bean by name, nothing special happens; use dependency-check="objects" to raise an error in that case. 3. "byType" Autowiring if there is exactly one bean of the property type in the bean factory. If there is more than one, a fatal error is raised, and you can't use byType autowiring for that bean. If there is none, nothing special happens; use dependency-check="objects" to raise an error in that case. 4. "constructor" Analogous to "byType" for constructor arguments. If there isn't exactly one bean of the constructor argument type in the bean factory, a fatal error is raised. 5. "autodetect" Chooses "constructor" or "byType" through introspection of the bean class. If a default constructor is found, "byType" gets applied. The latter two are similar to PicoContainer and make bean factories simple to configure for small namespaces, but doesn't work as well as standard Spring behaviour for bigger applications. Note that explicit dependencies, i.e. "property" and "constructor-arg" elements, always override autowiring. Autowire behavior can be combined with dependency checking, which will be performed after all autowiring has been completed. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.
意思是如果被bean中暴露出來一個bean后,如果找不到被暴露出來的這個bean,不會報錯,但是如果使用dependency-check="objects"則會報錯,但是我是什么都沒有暴露他出的錯誤啊,又仔細看發現mode ‘no’,人家明顯寫着The traditional Spring default.說默認是no,就是說默認不會自動wire,必須在xml文件中手動注入,手動連接,這下就明白了,原來沒寫default-autowire="byName"的時候是找不到sqlMapClient的,因為默認使用的是no!
后來我就試了一下byType完全沒有影響啊,估計是因為我的id都是唯一的,其他的就沒有再試了,有興趣大家可以自己試。
這下就對這個錯誤了解比較深了,也對spring配置有所了解,希望對碰見這個錯誤的同學有所幫助。
本文源自:http://www.cnblogs.com/mecca/p/3499449.html