spring aop事件两种实现方式,带注解和不带注解

这里写自定义目录标题

文件目录以及相应模块

spring aop事件两种实现方式,带注解和不带注解

service层

1.UserService接口

package com.ysq.service;
import com.ysq.model.User;
public interface UserService {
    public void addUser();
    public void updateUser();
    public void deleteUser();
    public int deleteUser(int id);
}

2.UserServiceImpl

ackage com.ysq.service;
import org.springframework.stereotype.Service;

@Service("userService")(不用注解的时候可以去掉,使用注解时必须加上)
public class UserServiceImpl implements UserService{
    @Override
    public void addUser() {
        System.out.println("添加用户。。。。");
    }
    @Override
    public void updateUser() {
        System.out.println("更新用户。。。。");
    }
    @Override
    public void deleteUser() {
        System.out.println("删除用户。。。。");
    }
    @Override
    public int deleteUser(int id) {
        System.out.println("通过id删除用户");
        return 1;
    }
}

一、不带注解的方式

1、 切面层代码MyAspect01

package com.ysq.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

//aop无注解方式
public class MyAspect01 {
    //前置通知
    public void myBefore(JoinPoint joinPoint){
        System.out.println("1.前置通知..." + joinPoint.getSignature().getName());
    }
    /**
     * 后置通知获取service方法执行后的返回值
     * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning
     * @param
     */
    public void myAfterReturning(JoinPoint joinPoint,Object retValue){
        System.out.println("3.后置通知..." +  joinPoint.getSignature().getName());
        System.out.println("返回值:" + retValue);
    }
    //环绕通知
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("2.环绕通知。。。开启事务。。"+proceedingJoinPoint.getSignature().getName());
        Object retObj = proceedingJoinPoint.proceed();
        System.out.println("环绕通知提交事务。。。");
        return retObj;
    }
    //异常事件捕获
    public void myAfterThrowing(JoinPoint joinPoint,Throwable ex){
        System.out.println("异常通知..." + joinPoint.getSignature().getName() + "===" + ex.getMessage() );
    }
    //最终通知
    public void myAfter(JoinPoint jp){
        System.out.println("最终通知..." + jp.getSignature().getName());
    }
}

2、bean.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"
       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="userService" class="com.ysq.service.UserServiceImpl"></bean>

        <bean id="myAspect" class="com.ysq.aspect.MyAspect01"></bean>

<!--        配置aop-->
        <aop:config>
                <!-- aop:指定切面-->
                <aop:aspect ref="myAspect">
                        <!--定义一个切入点-->
                        <aop:pointcut id="myPointcut" expression="execution(* com.ysq.service.UserServiceImpl.*(..))"/>
                        <!-- 配置前置通知...-->
                        <aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before>
                        <!-- 配置后置通知...-->
                        <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
                        <!--配置环绕通知-->
                        <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>
                        <!-- 配置异常通知 throwing="ex" 值,是方法的参数名-->
                        <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="ex"/>
                        <!--配置最终通知:不管有没有异常,最终通知都会执行-->
                        <aop:after method="myAfter" pointcut-ref="myPointcut"/>
                </aop:aspect>
        </aop:config>
</beans>

二、带注解的方式

1、 带注解的切面层代码MyAspect01

package com.ysq.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect02 {
    //声明一个公共的切入点
   @Pointcut("execution(* com.ysq.service.UserServiceImpl.*(..))")
    public void myPointcut(){};
    //前置通知
    @Before("myPointcut()")
    public void myBefore(JoinPoint joinPoint){
        System.out.println("1.前置通知..." + joinPoint.getSignature().getName());
    }
    /**
     * 后置通知获取service方法执行后的返回值
     * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning
     * @param
     */
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint joinPoint,Object retValue){
        System.out.println("3.后置通知..." +  joinPoint.getSignature().getName());
        System.out.println("返回值:" + retValue);
    }
    //环绕通知
    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("2.环绕通知。。。开启事务。。"+proceedingJoinPoint.getSignature().getName());
        Object retObj = proceedingJoinPoint.proceed();
        System.out.println("环绕通知提交事务。。。");
        return retObj;
    }
    //异常事件捕获
    @AfterThrowing(pointcut = "myPointcut()",throwing = "ex")
    public void myAfterThrowing(JoinPoint jp,Throwable ex){
        System.out.println("异常通知..." + jp.getSignature().getName() + "===" + ex.getMessage() );
    }
    //最终通知
    @After("myPointcut()")
    public void myAfter(JoinPoint joinPoint){
        System.out.println("最终通知..." + joinPoint.getSignature().getName());
    }
}

2、带注解的bean.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<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"
       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">

<!--        配置扫描注解的位置-->
        <context:component-scan base-package="com.ysq"></context:component-scan>
<!--       配置aop注解-->
        <aop:aspectj-autoproxy/>
        <aop:config>
           <aop:aspect ref="myAspect02"></aop:aspect>
        </aop:config>
</beans>

上一篇:一个简单的性能拦截器


下一篇:Spring基础(AOP)