今天試著搭建 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"的配置,所以出錯了。