问题描述
- 碰到个很小白的问题,希望各位大神给抽空解决下
-
今天试着搭建 struts1+spring+ibatis框架,结果在项目启动时就一直报错,下面是我的配置文件:
<?xml version="1.0" encoding="GBK"?>
<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.0.xsd"<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true" > <!-- oracle --> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" /> <property name="username" value="wendy"/> <property name="password" value="123"/> </bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="sqlMapClientBase" abstract="true" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="/WEB-INF/config/sqlMapConfig.xml" /> <property name="dataSource" ref="com.shunde.spring.dataSource" />
<bean id="sqlMapClient" parent="sqlMapClientBase"></bean> <!-- 分页查询取总数据量使用 --> <bean id="sqlMapClientCount" parent="sqlMapClientBase"></bean> <!-- 分页查询取部分数据使用 --> <bean id="sqlMapClientPage" parent="sqlMapClientBase"></bean> <bean id="attributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> <property name="properties"> <props> <!--PROPAGATION_REQUIRED是事务传播行为 如果当前没有事务,就创建一个事务;如果已经存在事务,则加入事务 --> <!-- -Exception指定事务回滚规则 方法内部出现java.lang.Exception及其子类异常事务会自动回滚 可以用逗号分隔多个回滚规则。“-”前缀强制回滚,“+”前缀指定提交(这允许即使抛出unchecked异常时也可以提交事务) --> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="edit*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="init*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="getNext*">PROPAGATION_REQUIRED,-Exception</prop> <!--<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> --><prop key="transaction*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <bean id="abstractTxDefinition" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributeSource"> <ref bean="attributeSource"/> </property>
<bean id="stmStuInfoDaoImpl" class="com.ssi.sm.dao.impl.StmStuInfoDaoImpl"> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="stmStuInfoServiceTarget" class="com.ssi.sm.service.impl.StmStuInfoServiceImpl"> <property name="stmStuInfoDao" ref="stmStuInfoDaoImpl"/> </bean> <bean id="stmStuInfoServiceImpl" parent="abstractTxDefinition"> <property name="target" ref="stmStuInfoServiceTarget"/> </bean> <bean name="/stmStuInfoAction" class="com.ssi.sm.action.StmStuInfoAction"> <property name="stmStuInfoService" ref="stmStuInfoServiceImpl"/> </bean>
报的错是: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stmStuInfoServiceTarget' defined in ServletContext resource [/WEB-INF/config/transaction-define.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlMapClient' is required at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) 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:222) 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:728) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:802) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079) at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1068) at org.apache.catalina.core.StandardHost.start(StandardHost.java:822) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1060) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) at org.apache.catalina.core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:759) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.IllegalArgumentException: Property 'sqlMapClient' is required
at org.springframework.orm.ibatis.SqlMapClientTemplate.afterPropertiesSet(SqlMapClientTemplate.java:147)
at org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.checkDaoConfig(SqlMapClientDaoSupport.java:108)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 39 more
解决方案
根据错误信息显示,说是在创建名称为"stmStuInfoServiceTarget"的bean是出错了。
看上面这段配置,说是stmStuInfoServiceTarget对应的类"com.ssi.sm.service.impl.StmStuInfoServiceImpl"里面有个属性是"com.ssi.sm.dao.impl.StmStuInfoDaoImpl"类型,而"com.ssi.sm.dao.impl.StmStuInfoDaoImpl"中又有个属性是id为"sqlMapClient"的类型,但是在配置文件中找步到的id为"sqlMapClient"的配置,所以出错了。
解决方案二:
我靠,代码怎么贴不上去。算了,我说的就是楼主最后发的那段配置。
解决方案三:
public class StmStuInfoDaoImpl extends SqlMapClientDaoSupport implements IStmStuInfoDao
我是在实现类中继承了 SqlMapClientDaoSupport ,在这个类里面有id为"sqlMapClient"的配置
SqlMapClientDaoSupport.java中的方法
public final void setSqlMapClient(SqlMapClient sqlMapClient) {
if (!this.externalTemplate) {
this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
}
}