首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

spring事务如何用

2012-01-13 
spring事务怎么用使用spring事务的时候遇到一个莫名其妙的问题Java codeimport org.springframework.conte

spring事务怎么用
使用spring事务的时候遇到一个莫名其妙的问题

Java code
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test1 {    public void test() {        System.out.println("test1");        ApplicationContext ctx =                 new ClassPathXmlApplicationContext("bean.xml");//到这里就不能继续运行了        System.out.println(ctx);        Transaction ts = (Transaction)ctx.getBean("tsa");        System.out.println("test");        ts.insert("insert into user (id,name,password,age) value(10,'sadasd','12312313',11)");            }}

输出结果是:test1
这是什么原因,bean.xml在src目录中
bean.xml
XML code
<?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.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="dataSource" class="org.springframework.jdbc.datasource" destroy-method="close">    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>    <property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"></property>    <property name="user" value="root"></property>    <property name="password" value="root"></property>    <property name="maxPoolSize" value="40"></property>    <property name="minPoolSize" value="1"></property>    <property name="initialPoolSize" value="1"></property>    <property name="maxIdleTime" value="20"></property></bean><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="dataSource"></property></bean><bean id="tsa" class="com.daisy.dao.Transaction">    <property name="ds" value="dataSource"></property></bean><tx:advice id="txAdvice" transaction-manager="transactionManager">    <tx:attributes>        <tx:method name="*" propagation="REQUIRED" />    </tx:attributes></tx:advice><aop:config>    <aop:pointcut expression="* com.daisy.dao.Transaction.*(..)" id="daoCut"/>    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoCut"/></aop:config></beans>



Transaction.java
Java code
import org.apache.tomcat.jdbc.pool.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class Transaction {    private DataSource ds;    public DataSource getDs() {        return ds;    }    public void setDs(DataSource ds) {        this.ds = ds;    }    public void insert(String sql){        JdbcTemplate jt = new JdbcTemplate(ds);        jt.execute(sql);        System.out.println(sql);        //jt.execute(sql);    }} 



[解决办法]
路径不对! 你加个/试试,这个是找不到这个配置文件的问题!
[解决办法]
具体报什么错,贴出来!
[解决办法]
请帖错误~~~
[解决办法]
尝试其他加载XML方式试试
Java code
String path="WebRoot/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(path);
[解决办法]
xml文件的问题了,建议查看日志,然后检查下文件的格式,再看看是不是在初始化bean的时候,类文件没有set参数哦。
贴异常日志出来吧,这样确实很难回答哈。

热点排行