栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring之XML 配置AOP 事务管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring之XML 配置AOP 事务管理

目录

XML中配置AOP

XML中配置事务管理


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.配置切面 切入点 通知




    
    

    
    


    
    
        
            
            

            

            

            

            

            

        
    


        测试类

@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 {
}

        转账类:

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




    
    

    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
        
            
                
            
        
        
            
                
                    
                        
                            mysql
                            true
                        
                    
                
            
        
    

    
    
        
        
        
        
    

    
    
        
    

    
    
    
        
    

    
    
        
            
            
        
    

    
    
        
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/642773.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号