• Spring AOP


    AOP

    在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: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.xsd">
        <!--增强类-->
        <bean id="txManger" class="cn.jiedada._06xmlaop.TxManger"></bean>
        
        <bean id="userService" class="cn.jiedada._06xmlaop.service.impl.UserServiceImpl"></bean>
        <aop:config>
            <!--想要增强的类,切点设置-->
            <aop:pointcut id="pointcut" expression="execution(* cn.jiedada._06xmlaop.service.I*Service.*(..))"></aop:pointcut>
            <aop:aspect ref="txManger">
               <!-- <aop:before method="begin" pointcut-ref="pointcut"></aop:before>
                <aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning>
                <aop:after-throwing method="rollback" pointcut-ref="pointcut"></aop:after-throwing>
                <aop:after method="close" pointcut-ref="pointcut"></aop:after>-->
                <!--增强方法-->
                <aop:around method="around" pointcut-ref="pointcut"></aop:around>
            </aop:aspect>
        </aop:config>
    </beans>

    TxManger中的配置

    这样就可以了

    package cn.jiedada._06xmlaop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class TxManger {
        public void begin(){
            System.out.println("开启事务");
        }
        public void commit(){
            System.out.println("提交事务");
        }
        public void rollback(Throwable e){
            System.out.println("回滚事务"+e.getMessage());
        }
        public void close(){
            System.out.println("关闭事务");
        }
        public void around(ProceedingJoinPoint proceedingJoinPoint){
            try {
                begin();
                //获得传入的方法名
                proceedingJoinPoint.proceed();
                commit();
            }catch (Throwable e){
                rollback(e);
            }finally {
                close();
            }
        }
    }
    View Code

    这只是一个模板

  • 相关阅读:
    Java数据结构与算法(15)
    SpringJMS解析--使用示例
    Java数据结构与算法(14)
    Java数据结构与算法(13)
    Java数据结构与算法(12)
    如何实践设计原则-面向对象设计原则
    软件架构的两大流派:组成派、决策派
    软件 可变与不可变 及封装可变性
    Java数据结构与算法(11)
    搭建本地yum仓库及自制rpm包(无需镜像)
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11639363.html
Copyright © 2020-2023  润新知