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

34.6、 Spring

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

34.6、 Spring

 

 

 

 

package com.hhh.aspect.aop;

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

public class MyAspect {
    
    public void myBefore(JoinPoint joinPoint){

//        joinPoint.getTarget(); 获取目标对象
//        joinPoint.getSignature().getName(); 获取目标方法名
//        joinPoint.getArgs(); 获取目标方法参数列表
//        joinPoint.getThis(); 获取代理对象
        System.out.println("Before..."+joinPoint.getSignature().getName());
    }

    
    public void myAfterReturning(JoinPoint joinPoint){
        System.out.println("After..."+joinPoint.getSignature());
    }

    
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("Around Before"+proceedingJoinPoint.getSignature().getName());
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("Around After环绕通知"+proceedingJoinPoint);
        return obj;
    }

    
    public void myAfterThrowing(Exception e){
        System.out.println("Exception  "+e);
    }

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

 

 修改业务层:创建接口及实现类

 配置切面:





    


    


    
        

            

            

            

            

            

            
        
    

 测试:

 

 

 

package com.hhh.schema_based.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

public class basedMyAspect implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor, ThrowsAdvice {
    
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Around Before");
        Object obj = methodInvocation.proceed();
        System.out.println("Around After");
        return obj;
    }

    
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("After...");
    }

    
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("Before....");
    }

    
    public void afterThrowing(Exception ex){
        System.out.println("异常通知");
    }
}

 

 

 

 配置多切面:

 

package com.hhh.aop;

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

@Aspect //指定当前对象为切面对象
public class MyAspect {
    
    @Pointcut("execution(* com.hhh.service.*.*(..)")
    public void myPointcut(){

    }
    
//    @Before(value = "execution(* com.hhh.service.*.*(..)")
    @Before(value = "myPointcut")

    public  void myBefore(JoinPoint joinPoint){
        System.out.println("Before..."+joinPoint.getSignature().getName());
    }

    
//    @After(value = "execution(* com.hhh.service.*.*(..)")
    @After(value = "myPointcut")
    public void myAfterRetuning(JoinPoint joinPoint){
        System.out.println("AfterReturning "+joinPoint.getSignature().getName());
    }

    
    @Around("myPointcut")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("Around Before");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("Around After");
        return proceed;
    }

    
//    @After("execution(* com.hhh.service.*.*(..)")
    @After("myPointcut")
    public  void  myAfter(){
        System.out.println("最终通知");
    }

    
//    @AfterThrowing(value = "execution(* com.hhh.service.*.*(..)",throwing = "e")
    @AfterThrowing(value = "myPointcut",throwing = "e")
    public void myAfterThrowing(Exception e){
        System.out.println("有异常 "+e);
    }
}

创建目标对象:接口及实现类

 

 配置注解式的切面;

 

package com.hhh.aop;

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

@Aspect //指定当前对象为切面对象
public class MyAspect {
    
    @Pointcut("execution(* com.hhh.service.*.*(..))")
    public void myPointcut(){

    }
    
//    @Before(value = "execution(* com.hhh.service.*.*(..)")
    @Before(value = "myPointcut()")
    public  void myBefore(JoinPoint joinPoint){
        System.out.println("Before..."+joinPoint.getSignature().getName());
    }

    
//    @After(value = "execution(* com.hhh.service.*.*(..)")
    @After(value = "myPointcut()")
    public void myAfterRetuning(JoinPoint joinPoint){
        System.out.println("AfterReturning "+joinPoint.getSignature().getName());
    }

    
    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("Around Before");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("Around After");
        return proceed;
    }

    
//    @After("execution(* com.hhh.service.*.*(..)")
    @After("myPointcut()")
    public  void  myAfter(){
        System.out.println("最终通知");
    }

    
//    @AfterThrowing(value = "execution(* com.hhh.service.*.*(..)",throwing = "e")
    @AfterThrowing(value = "myPointcut()",throwing = "e")
    public void myAfterThrowing(Exception e){
        System.out.println("有异常 "+e);
    }
}

 创建测试类:

 

 新创建一个切面:

 配置这个新添加的切面:

 注意多切面执行的方法:

 由于切面中@Order小的先执行:

 注意执行的方式:先执行Order小的的环绕前置然后前置然后Order大的的前置然后方法然后后置。。。总之他是一个环形

 

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

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

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