AOP面向切面的原生态写法

第一种:
com.offcn.aop里面的AopError是增强bean
AOP面向切面的原生态写法_第1张图片
增强bean代码

package com.offcn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class AopError {
    // JoinPoint这个类有你动态代理类的信息
    public void afterError(JoinPoint joinPoint,RuntimeException e){
        //joinPoint.getSingnature() 这个方法是拿到你的动态代理的前面文件
        System.out.println(joinPoint.getSignature().getName()+"这个方法的异常信息"+e.getMessage());
    }
    public void after(JoinPoint jionPoint){
        System.out.println(jionPoint.getSignature().getName());

    }
    //环绕增强 传递的是其子类
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs()));
        //调用此方法执行目标对象相应的方法 这个方法相当于一个分割线,也就是前置增强和
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println(proceedingJoinPoint.getSignature().getName());

    }
}

设置xml,代码如下

package com.offcn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import java.util.Arrays;
//设置切面的注解
@Aspect

public class AopError {
    // JoinPoint这个类所有你动态代理类的信息
    @AfterThrowing(pointcut = "execution(* com.offcn.service.UserService.*(..)",throwing = "e")
    public void afterError(JoinPoint joinPoint,RuntimeException e){
        //joinPoint.getSingnature() 这个方法是拿到你的动态代理的前面文件
        System.out.println(joinPoint.getSignature().getName()+"这个方法的异常信息"+e.getMessage());
    }
    @After("execution(* com.offcn.service.UserService.*(..))")
    public void after(JoinPoint jionPoint){
        System.out.println(jionPoint.getSignature().getName());

    }
    @Before("execution(* com.offcn.service.UserService.*(..))")
    public void before(JoinPoint jionPoint){
        System.out.println(jionPoint.getSignature().getName());

    }

    //环绕增强 传递的是其子类
    @Around("execution(* com.offcn.service.UserService.*(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs()));
        //调用此方法执行目标对象相应的方法 这个方法相当于一个分割线,也就是前置增强和
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println(proceedingJoinPoint.getSignature().getName());

    }
}

第二种注解方式:
1.使用注解方式xml不需要自己配置,只需要开启spring注解
2.XML文件,多了三个AOP的头

增强bean代码

package com.offcn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import java.util.Arrays;
//设置切面的注解
@Aspect

public class AopError {
    // JoinPoint这个类所有你动态代理类的信息
    @AfterThrowing(pointcut = "execution(* com.offcn.service.UserService.*(..)",throwing = "e")
    public void afterError(JoinPoint joinPoint,RuntimeException e){
        //joinPoint.getSingnature() 这个方法是拿到你的动态代理的前面文件
        System.out.println(joinPoint.getSignature().getName()+"这个方法的异常信息"+e.getMessage());
    }
    @After("execution(* com.offcn.service.UserService.*(..))")
    public void after(JoinPoint jionPoint){
        System.out.println(jionPoint.getSignature().getName());

    }
    @Before("execution(* com.offcn.service.UserService.*(..))")
    public void before(JoinPoint jionPoint){
        System.out.println(jionPoint.getSignature().getName());

    }

    //环绕增强 传递的是其子类
    @Around("execution(* com.offcn.service.UserService.*(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs()));
        //调用此方法执行目标对象相应的方法 这个方法相当于一个分割线,也就是前置增强和
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println(proceedingJoinPoint.getSignature().getName());

    }
}

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-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

	<context:component-scan base-package="com.offcn.dao.impl,com.offcn.service.dao.impl" />
	<bean class="com.offcn.aop.AopError"></bean>
	<!--开启spring注解-->
	<aop:aspectj-autoproxy/>
</beans>

设置切面的注解
@Aspect
抛出异常的方式注解:
@AfterThrowing(pointcut=“execution(* com.offcn.service.UserService.(…))" ,throwing=“e” )
环绕增强
@Around("execution( com.offcn.service.UserService.(…))”)
后置注解
@After("execution( com.offcn.service.UserService.
(…))”)
前置注解
@Before("execution( com.offcn.service.UserService.*(…))”)

你可能感兴趣的:(AOP面向切面的原生态写法)