在进行 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