SSH配置bean注入及获取的方法

本文详细解析了Spring框架中因依赖注入类型不匹配导致的启动错误,阐述了正确的配置方式和解决策略,强调了面向接口编程的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

启动报错:Error creating bean with name ‘userServiceImpl’ defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.Proxy10implementingdemo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised′torequiredtype′demo.ssh.daoImpl.UsersDaoImpl′forproperty′dao′;nestedexceptionisjava.lang.IllegalStateException:Cannotconvertvalueoftype[com.sun.proxy.Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'demo.ssh.daoImpl.UsersDaoImpl' for property 'dao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.Proxy10implementingdemo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advisedtorequiredtypedemo.ssh.daoImpl.UsersDaoImplforpropertydao;nestedexceptionisjava.lang.IllegalStateException:Cannotconvertvalueoftype[com.sun.proxy.Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.ssh.daoImpl.UsersDaoImpl] for property ‘dao’: no matching editors or conversion strategy found

完整报错代码:

八月 05, 2019 4:14:45 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'demo.ssh.daoImpl.UsersDaoImpl' for property 'dao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.ssh.daoImpl.UsersDaoImpl] for property 'dao': no matching editors or conversion strategy found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4579)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5041)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:742)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:718)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1142)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1876)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
	at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
	at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1054)
	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:428)
	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1586)
	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:308)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
	at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:424)
	at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:367)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:966)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
	at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:353)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:493)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'demo.ssh.daoImpl.UsersDaoImpl' for property 'dao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.ssh.daoImpl.UsersDaoImpl] for property 'dao': no matching editors or conversion strategy found
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:591)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:204)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1527)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	... 52 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy10 implementing demo.ssh.dao.UsersDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.ssh.daoImpl.UsersDaoImpl] for property 'dao': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:302)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
	... 58 more

原因:很明显,配置文件生成bean错误,是什么原因呢?原来是获取类的时候出错。Spring是面向接口编程,所以不能用实 现类来得到bean,而应该用接口得到bean对象。

applicationContext配置:

<bean id="usersDaoImpl" class="demo.ssh.daoImpl.UsersDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userServiceImpl" class="demo.ssh.serviceImpl.UserServiceImpl">
		<property name="dao" ref="usersDaoImpl"></property>
</bean>

获取bean:

public class UserServiceImpl implements UserService{  
    //用接口,不能用实现类
	private UsersDao dao; 
    public void setDao(UsersDao dao) {
		this.dao = dao;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪悟道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值