我按照spring文档中的步骤建立了如上两个文件。然后使用事务时,如果我讲bean声明都写在applicationContext中那么可以回滚,但是如果我他们分开的话就不可以了。
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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
//配置数据源
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
//配置事务管理器以及为管理器配置属性和定义对哪些东西进行aop
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.RuntimeException"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="fooServiceOperation"
expression="execution(* mbp.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>//ibatis配置,创建模版以及将模版植入UserDao类中
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean> 
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>  
<bean id="userDao" class="mbp.dao.UserDao">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>         //service类,调用UserDao
<bean id="userservice" class="mbp.service.UserService">
<property name="userdao" ref="userDao" />
</bean>
        //struts1.x的action继承了DispatchAction
<bean name="/user" class="mbp.com.UserAction">
<property name="userservice" ref="userservice"></property>
</bean>


<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/jdbc.properties</value>
</list>
</property>
</bean></beans>
以上代码都放在applicationContext.xml中没有问题。
然后我将他们拆分
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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.RuntimeException"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="fooServiceOperation"
expression="execution(* mbp.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean> 
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>  
<bean id="userDao" class="mbp.dao.UserDao">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/jdbc.properties</value>
</list>
</property>
</bean></beans>action-servlet.xml:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans>
 
<bean name="/user" class="mbp.com.UserAction">
<property name="userservice" ref="userservice"></property>
</bean> //我感觉就是因为我这里拿到UserDao类的和applicationContext.xml中的不是同一个实例照成的,
//但是不是很明确。因为如果拿到的不一样,那么这里的应该没有拿到数据源以及sqlmapclient.
//如果不用事务回滚,也是能够查询删除什么的
<bean id="userdao" class="mbp.dao.UserDao"></bean>

<bean id="userservice" class="mbp.service.UserService">
<property name="userdao" ref="userdao"/>
</bean></beans>1。我感觉就是因为action-servlet.xml中的<bean id="userdao" class="mbp.dao.UserDao"></bean>这句照成的,感觉这里拿到UserDao类的和applicationContext.xml中的不是同一个实例,但是不是很明确。因为如果拿到的不一样,那么这里的应该没有拿到数据源以及sqlmapclient.那么我程序里service类怎么执行的呢,怎么可能增删改查呢?如果不用事务回滚,也是能够查询删除什么的。
2.还有一个问题就是这两个.xml文件到底应该或者大叫都是怎么分配bean的编写的,什么bean写在什么文件中呢?
小弟第一次发帖,心情紧张,还希望各位高手畅所欲言讲讲各位的看法springibatisstruts事务回滚aop配置文件

解决方案 »

  1.   

    默认配置文件是applicationContext.xml
    需要配置多文件,要么在主文件中直接import,要么在其它文件中声明另外,为什么你分开之后要换文件头呢?
      

  2.   

     2.还有一个问题就是这两个.xml文件到底应该或者大叫都是怎么分配bean的编写的,什么bean写在什么文件中呢?第二个问题:之前用ssh的时候  action service和dao分别写在三个不同的xml文件里  不过现在用注解 少了很多了
      

  3.   


    我把问题讲的简单一点吧。
    在applicationContext中我配置了一个已经拿到数据源的dao,我想在action-servlet.xml中拿到那个已经拿到数据源的dao,该怎么拿呢?
      

  4.   


    我把问题讲的简单一点吧。
    在applicationContext中我配置了一个已经拿到数据源的dao,我想在action-servlet.xml中拿到那个已经拿到数据源的dao,该怎么拿呢?在a中import b文件,其余就把他们当作一个文件。
      

  5.   


    我把问题讲的简单一点吧。
    在applicationContext中我配置了一个已经拿到数据源的dao,我想在action-servlet.xml中拿到那个已经拿到数据源的dao,该怎么拿呢?在a中import b文件,其余就把他们当作一个文件。
    请问怎么在xml文件中import另一个xml文件呢?愿闻其祥。
      

  6.   

    再加一楼。
    1.我终于知道怎么做了,直接引用applicationContext.xml文件中的id属性值就可以了,两个文件是相通的,action-servlet.xml继承了applicationContext.xml文件,之所以相通是因为我在struts-config.xml中配置了个ContextLoaderPlugIn插件,这个插件是为struts的actionservlet加载spring context文件(也就是applicationContext.xml),并将其作为父类。
    2.虽然action-servlet.xml中可以配置任何的bean,但是就像我上面这样的,拿到事务的dao配置在applicationContext.xml中,然后我错误的以为我在action-servlet.xml中再配一个dao也是拿到事务的,其实这样就是两个dao了,然后我在action中拿到的是action-servlet.xml中的dao,也就是没有事务的dao,所以事务不成功。
    总结:applicationContext.xml中配置数据源啊,事务管理器啊,以及需要实例化的类,需要aop的类。
    action-servlet.xml中配置struts.xml中的action,需要注入实例,直接<property name="searchservice" ref="xx"/>,xx就是applicationContext.xml中配置的实例的id,直接拿就可以了,不要客气。
    =====================
    谢谢两位,晚上有空就结贴