jdk动态代理与spring事务环境搭建

一、配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.geely</groupId>
    <artifactId>spring-pro</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.20.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
    </dependencies>

</project>

 

二、编写spring配置类

package com.geely.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author caopeng
 * @className AppConfig
 * @date 2025-05-23 16:19
 */
@Configuration
// 开始事情
@EnableTransactionManagement
@ComponentScan("com.haojie")
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL");
        dataSource.setUsername("test");
        dataSource.setPassword("sfdfsfsdf");
        return dataSource;
    }

    @Bean
    public TransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public JdbcTemplate  jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

 

三、创建测试类

UserService

package com.haojie.service.inf;

public interface UserService {

    void doAddStudent();

    void addStudent();

}

 

UserServiceImpl

package com.haojie.service.impl;

import com.geely.service.inf.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author caopeng
 * @className UserServiceImpl
 * @date 2025-05-26 09:54
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional
    public void doAddStudent() {
        String name = this.getClass().getName();
        System.out.println("doAddStudent className="+name);
        jdbcTemplate.update("insert into student(stu_no,stu_name) values(?,?)", "001", "张三");
        throw new RuntimeException("发生异常");
    }

    @Override
    public void addStudent() {
        String name = this.getClass().getName();
        System.out.println("doAddStudent className="+name);
        doAddStudent();
    }


}

 

AppMain

package com.haojie.test;

import com.haojie.config.AppConfig;
import com.haojie.service.inf.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author caopeng
 * @className AppMain
 * @date 2025-05-23 16:43
 */
public class AppMain {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = context.getBean(UserService.class);
        System.out.println(userService.getClass().getName());
        //userService.addStudent();
        userService.doAddStudent();
        context.close();
    }
}

 

posted @ 2025-05-27 19:08  阿瞒123  阅读(4)  评论(0)    收藏  举报