spring复习:(42)配置文件的方式实现事务(TransactionProxyFactoryBean)

一、定义服务接口:

package cn.edu.tju.study.service.transaction2;

public interface StudentService {
    public void addStudent(String name, int age);
}

二、定义服务实现类

package cn.edu.tju.study.service.transaction2;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService{
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void addStudent(String name,int age) {
        String sql = "insert into student values('";
        sql += name;
        sql +="'";
        sql += ",";
        sql += age;
        sql += ")";
        jdbcTemplate.execute(sql);
        int i = 1/0;
        jdbcTemplate.execute(sql);

    }
}

三、配置文件:

<?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:tx="http://www.springframework.org/schema/tx"
       default-autowire="byName"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="MyPa"></property>

    </bean>
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>



    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="studentService" class="cn.edu.tju.study.service.transaction2.StudentServiceImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>


    <bean id="myFactoryBean"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="target" ref="studentService" />
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>


</beans>

四、主类,调用TransactionProxyFactoryBean

package cn.edu.tju.study.service.transaction2;

import cn.edu.tju.study.service.MyConfig3;
import cn.edu.tju.study.service.aspect.MyDemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("transaction.xml");

        StudentService studentService = context.getBean("myFactoryBean", StudentService.class);

        studentService.addStudent("paul",23);
    }
}

猜你喜欢

转载自blog.csdn.net/amadeus_liu2/article/details/131794465