欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring 配置xml注入bean同名异常的问题

程序员文章站 2022-05-28 14:15:40
...

应用环境:使用了struts2、spring2.5和ibatis等。

问题重现:xml配置文件如下,

	<bean id="logAction" class="packageName.LogAction" scope="prototype">
		<property name="logManager" ref="logManager"/>
	</bean>
	<bean id="logManager" class="packageName.LogManagerImpl" scope="prototype">
		<property name="logDao" ref="logDao"/>	
	</bean>
	
	<bean id="logDao" class="packageName.LogDaoiBatis" scope="prototype">
		<property name="sqlMapClient" ref="sqlMapClient"/>	
	</bean>

由于整个项目中有很多spring配置文件,这里的logManager 在别的地方也有配置,且指定的另外的一个类,在应用启动的时候,还没有报错(我想spring 只是检查,没有真正注入吧,还是有相关配置?),在用到该模块的时候,会报如下错误(大概意思是logAction无法为logManager这个属性注入bean):

Unable to instantiate Action, logAction,  defined for 'queryLogs' in namespace 'ExampleNamespace'Error creating bean with name 'logAction' defined in ServletContext resource beanA.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy6] to required type [packageName.log.LogManager] for property 'logManager'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy6] to required type [packageName.log.LogManager] for property 'logManager': no matching editors or conversion strategy found - action - file:/F:/path/strutsConf.xml:9:71

 ps:有些命名空间和文件名做了修改。strutsConf.xml某个模块的struts配置文件。

 

 

用下面配置方式就解决了重名的问题:

<bean id="logAction" class="packageName.LogAction" scope="prototype">
		<property name="logManager">
			<bean class="packageName.log.LogManagerImpl">
				<property name="logDao">
					<bean  class="packageName.log.LogDaoiBatis" scope="prototype">
						<property name="sqlMapClient" ref="sqlMapClient"/>	
					</bean>
				</property>
			</bean>
		</property>	
	</bean>

 

只做个记录,研究的比较浅,有了解的请指教,呵呵 O(∩_∩)O~