Aspect类
package com.sy.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* 切面类:只要编写一个普通的pojo类即可
*/
@Component("myAspect")
//标记当前类是一个切面类
@Aspect
public class MyAspect {
@Pointcut("bean(*ServiceImpl)")
public void method() {
//该方法中不需要实现任何的功能,目的是为了能够在
//方法上打上@Pointcut注解,从而声明切面对应的切入点
//后面具体的通知方法中,通过method() 来指定这个方法,从而获悉切入点
}
/**
* 此时的value相当于xml中的poincut-ref,指定了切入点
*/
@Before(value = "method()")
public void beforeAdvice(JoinPoint joinpoint) {
System.out.println("前置通知...");
System.out.println("方法名:" + joinpoint.getSignature().getName());
System.out.println("方法参数:" + Arrays.toString(joinpoint.getArgs()));
}
/**
* @param joinPoint
* @param returnVal 目标方法调用完以后的返回值
*/
@AfterReturning(value = "method()", returning = "returnVal")
public void afterAdvice(JoinPoint joinPoint, Object returnVal) {
System.out.println("后置通知");
System.out.println("方法名称:" + joinPoint.getSignature().getName());
System.out.println("方法参数:" + Arrays.toString(joinPoint.getArgs()));
System.out.println("返回值:" + returnVal);
System.out.println("=====================================================");
}
@Around(value = "method()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知的前置部分...");
//环绕通知中,目标方法需要通过ProceedingJoinPoint来调用
Object result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("result:" + result);
System.out.println("环绕通知的后置部分...");
//环绕通知的返回值可以决定最终方法调用后的返回值
//return false;
return result;
}
@AfterThrowing(value = "method()", throwing = "e")
public void afterThrowingAdvice(JoinPoint joinPoint, Throwable e) {
System.out.println("发生了异常...");
System.out.println("错误信息:" + e.getMessage());
}
@After(value = "method()")
public void finallyAdvice(JoinPoint joinPoint) {
System.out.println("最终通知");
}
}
AspectJ注解方式.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.sy"/>
<!--
配置一个对@Aspect进行注解扫描的节点
-->
<aop:aspectj-autoproxy/>
</beans>
测试
package com.sy.test;
import com.sy.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:AspectJ注解方式.xml")
public class Test01 {
@Autowired
private UserService userService;
@Test
public void test01() {
userService.login("Tom", "123456");
}
}
结果