spring整合ibatis

lijian8552 2011-05-10 03:56:13
刚不小心发错论坛了!我的问题如下:
不知道怎么了,老是报错。定义其他的bean都没有问题。报错如下
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ustcori.familyfinancing.dao.database.UserDAOImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ustcori.familyfinancing.dao.database.UserDAOImpl.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:881)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:837)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.ustcori.familyfinancing.Test.main(Test.java:29)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ustcori.familyfinancing.dao.database.UserDAOImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ustcori.familyfinancing.dao.database.UserDAOImpl.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:875)
... 16 more
Caused by: java.lang.NoSuchMethodException: com.ustcori.familyfinancing.dao.database.UserDAOImpl.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:54)
... 17 more


applicationContext.xml内容如下:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location">
<value>classpath:init.properties</value>
</property>
</bean>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
</property>
<property name="user">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<!-- 初始化时获取连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<!-- 连接池中保留的最小连接数 -->
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<!-- -连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<!-- 连接最大空闲时间,单位为秒。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<!-- 检查所有连接池中的空闲连接的时间间隔,单位是秒。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。 -->
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<!-- -c3p0支持的线程数,c3p0是异步操作的,通过多线程实现多个操作同时被执行。Default: 3- -->
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>SqlMapConfig_MySQL.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userDao"
class="com.ustcori.familyfinancing.dao.database.UserDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="user"
class="com.ustcori.familyfinancing.model.database.User">
</bean>
</beans>

测试类:
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");

UserDAO userDao = (UserDAOImpl) factory.getBean("userDao");

}


其中UserDAO和UserDAOImpl都是用abator自动生成的.
...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijian8552 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 panhaichun 的回复:]
com.ustcori.familyfinancing.dao.database.UserDAOImpl
这个类没有默认构造函数。

你要么使用构造函数注入,要么用默认构造函数。
[/Quote]

谢谢你的回答!问题解决了!
wangjn1982 2011-05-10
  • 打赏
  • 举报
回复
不能这么写吧

UserDAO userDao = (UserDAOImpl) factory.getBean("userDao");

应该是

UserDAO userDao = (UserDAO)factory.getBean("userDao");
panhaichun 2011-05-10
  • 打赏
  • 举报
回复
com.ustcori.familyfinancing.dao.database.UserDAOImpl
这个类没有默认构造函数。

你要么使用构造函数注入,要么用默认构造函数。
lijian8552 2011-05-10
  • 打赏
  • 举报
回复
自己顶!
lijian8552 2011-05-10
  • 打赏
  • 举报
回复
怎么没人来回答啊?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧