SSH出现问题的解决方法

之前布置好了SSH框架,在做项目后运行却遇到了问题,启动Tomcat服务器的时候报错:

严重: 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 'loginAction' 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.$Proxy15 implementing demo.dao.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'demo.dao.impl.UserDaoImpl' for property 'dao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy15 implementing demo.dao.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.dao.impl.UserDaoImpl] for property 'dao': no matching editors or conversion strategy found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy15 implementing demo.dao.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'demo.dao.impl.UserDaoImpl' for property 'dao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy15 implementing demo.dao.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.dao.impl.UserDaoImpl] for property 'dao': no matching editors or conversion strategy found
	at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	... 24 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy15 implementing demo.dao.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [demo.dao.impl.UserDaoImpl] for property 'dao': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
	at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
	... 30 more
很明显,配置文件生成bean错误,是什么原因呢?

弄了很久才知道,原来是使用类的时候出错。Spring是面向接口编程,所以不能用实现类来得到bean,而应该用接口得到bean对象。

如下是applicationContext配置文件的bean:

<bean name="userDao" class="demo.dao.impl.UserDaoImpl">
	<property name="factory" ref="sessionFactory"></property>
</bean>
<bean name="loginAction" class="demo.action.LoginAction">
	<property name="dao" ref="userDao"></property>
</bean>
LoginAction类错误的注入:

</pre><pre name="code" class="html"><pre name="code" class="html">public class LoginAction {
	private UserDaoImpl dao;
	public void setDao(UserDaoImpl dao) {
		this.dao = dao;
	}......

 
LoginAction类正确的注入(UserDao是接口): 

public class LoginAction {
	private UserDao dao;
	public void setDao(UserDao dao) {
		this.dao = dao;
	}......
问题解决,再次运行服务器就成功了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值