Spring之XML 配置AOP 事务管理


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());

    }

}

目标类


public class UserServiceImpl implements UserService {



    public void eat(){

        int i = 1 / 0;

        System.out.println("吃饭");

    }

}

xml:

步骤:

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

2.配置切面类

3.配置切面 切入点 通知


<?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>

测试类


@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:


@Entity(name = "account")

public class Account {

    @Id

    private Integer id;

    private String name;

    private Float money;

mapper:


public interface AccountMapper extends Mapper<Account> {

}

转账类:


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);

    }

}

测试类:


@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配置类:


<?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



### 最后

光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的**增加文章的篇幅**,减少文章的可读性

# Java面试宝典2021版

![](https://img-blog.csdnimg.cn/img_convert/09ea19e20b1791314df6ba536c445255.webp?x-oss-process=image/format,png)

![](https://img-blog.csdnimg.cn/img_convert/7d7edb94e23ce000cf16401539b0732b.webp?x-oss-process=image/format,png)

# 最常见Java面试题解析(2021最新版)

![](https://img-blog.csdnimg.cn/img_convert/fc841c6840101b32f412d334bc077615.webp?x-oss-process=image/format,png)

![](https://img-blog.csdnimg.cn/img_convert/8199891b377a02cf411d93d47df7c4c3.webp?x-oss-process=image/format,png)

# 2021企业Java面试题精选

![](https://img-blog.csdnimg.cn/img_convert/b4e3e931b454d3a888670d4b6a0a5b87.webp?x-oss-process=image/format,png)

![](https://img-blog.csdnimg.cn/img_convert/a2ce47fd2a850ed49c020b8ee6259968.webp?x-oss-process=image/format,png)

               http://www.springframework.org/schema/mvc



### 最后

光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的**增加文章的篇幅**,减少文章的可读性

# Java面试宝典2021版

[外链图片转存中...(img-zK0rliPA-1718722011571)]

[外链图片转存中...(img-4Fk1J62D-1718722011572)]

# 最常见Java面试题解析(2021最新版)

[外链图片转存中...(img-X37Qyrzi-1718722011572)]

[外链图片转存中...(img-6z0qevqK-1718722011572)]

# 2021企业Java面试题精选

[外链图片转存中...(img-1ddMrI2E-1718722011573)]

[外链图片转存中...(img-vHgOp6mZ-1718722011573)]

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值