前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring之XML 配置AOP 事务管理

Spring之XML 配置AOP 事务管理

作者头像
陶然同学
发布2023-02-24 09:39:31
2660
发布2023-02-24 09:39:31
举报
文章被收录于专栏:陶然同学博客

目录

XML中配置AOP

XML中配置事务管理


XML中配置AOP

        切面类 正常写通知 不用加注解

代码语言:javascript
复制
public class MyAspect {

    //前置通知
    public void mybefore(){
        System.out.println("前置通知");
    }

    //后置通知
    public void myaftereturning(Object obj){
        System.out.println("后置通知");
    }

    //环绕通知
    public void myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知11");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕通知11");
    }

    //最终通知
    public void myafter(){
        System.out.println("最终通知");
    }

    //异常通知
    public void myafterThrowing(Throwable e){
        System.out.println("异常通知");
        System.out.println(e.getMessage());
    }
}

        目标类

代码语言:javascript
复制
public class UserServiceImpl implements UserService {

    public void eat(){
        int i = 1 / 0;
        System.out.println("吃饭");
    }
}

        xml:        

        步骤:        

                1.配置UserServiceImpl 将UserServcieImpl放入Spring容器 

                2.配置切面类 

                3.配置切面 切入点 通知

代码语言:javascript
复制
<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置userService 将UserServiceImpl放入SpringIoc容器-->
    <bean id = "userService" class = "com.czxy.demo04.service.impl.UserServiceImpl"></bean>

    <!--配置切面类-->
    <bean id = "myAspect" class="com.czxy.demo04.aspect.MyAspect"></bean>


    <!--配置切面-->
    <aop:config>
        <aop:aspect ref = "myAspect">
            <!--切入点-->
            <aop:pointcut id = "myPointcut" expression = "execution(* com.czxy.demo04.service.impl.UserServiceImpl.*(..))"></aop:pointcut>

            <aop:before method = "mybefore" pointcut-ref = "myPointcut"></aop:before>

            <aop:after-returning method="myaftereturning" pointcut-ref="myPointcut" returning="obj"></aop:after-returning>

            <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>

            <aop:after method="myafter" pointcut-ref="myPointcut"></aop:after>

            <aop:after-throwing method="myafterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>

        </aop:aspect>
    </aop:config>


</beans>

        测试类

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:demo04.xml"})
public class TestA {

    @Resource(name = "userService")
    private UserService userService;

    @Test
    public void test01(){
        userService.eat();
    }
}

XML中配置事务管理

        转账 当出现错误的时候 转账失败 并且余额不变

        xml配置类步骤:

                1.加载属性配置文件

                2.配置连接池

                3.配置sessionFactory

                4.扫描dao的包

                5.配置service

                6.定义事务管理器

                7.配置事务属性

                8.配置事务切入点

        domain:

代码语言:javascript
复制
@Entity(name = "account")
public class Account {
    @Id
    private Integer id;
    private String name;
    private Float money;

        mapper:

代码语言:javascript
复制
public interface AccountMapper extends Mapper<Account> {
}

        转账类:

代码语言:javascript
复制
public class AccountServiceImpl implements AccountService {

    private AccountMapper accountMapper;

    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    public void change(Integer outId, Integer inId, Float money) {
        Account outAccount = accountMapper.selectByPrimaryKey(outId);
        outAccount.setMoney(outAccount.getMoney() - money);
        accountMapper.updateByPrimaryKey(outAccount);

         int i = 1 / 0;

        Account inAccount = accountMapper.selectByPrimaryKey(inId);
        inAccount.setMoney(inAccount.getMoney() + money);
        accountMapper.updateByPrimaryKey(inAccount);
    }
}

        测试类:

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:demo05.xml")
public class TestA {

    @Resource(name = "accountService")
    private AccountService accountService;

    @Test
    public void testDemo(){
        accountService.change(1,2,3f);
        System.out.println("转账成功");
    }
}

        xml配置类:

代码语言:javascript
复制
<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 1.1 指定配置文件的位置-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 1.2 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 配置初始化大小、最小、最大连连接数量 -->
        <property name="initialSize" value="5"/>
        <property name="minIdle" value="10"/>
        <property name="maxActive" value="20"/>
    </bean>

    <!-- 2.1 sessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.czxy.demo05.domain" />
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <property name="mapUnderscoreToCamelCase" value="true" />
            </bean>
        </property>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <props>
                            <prop key="dialect">mysql</prop>
                            <prop key="rowBoundsWithCount">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 2.2 扫描Dao的包 -->
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定会话工厂 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 指定 mybatis 接口所在的包 -->
        <property name="basePackage" value="com.czxy.demo05.mapper"/>
    </bean>

    <!-- 3 配置service -->
    <bean id="accountService" class="com.czxy.demo05.service.impl.AccountServiceImpl">
        <property name="accountMapper" ref="accountMapper" />
    </bean>

    <!-- 4 配置事务 -->
    <!-- 4.1 定义事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 4.2 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="change" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 4.3 配置事务切入点 -->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.czxy.demo05.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
    </aop:config>

</beans>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-08-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • XML中配置AOP
  • XML中配置事务管理
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档