@AspectJ注解的说明:
- @Aspect:切面
- @Pointcut:切入点
- @Before:前置通知
- @AfterReturning:后置通知
- @AfterThrowing:异常通知
- @Around:环绕通知:
- @After:最终通知,不管是否异常,都会执行
- 项目所需要的jar包
链接:jar包资源
提取码:mrcr - 创建包com.ssm.service,包中创建接口ProductService。
package com.ssm.service;
public interface ProductService {
public void browseBefore(String loginName, String ProductName);
public void browseAfterReturn(String loginName, String ProductName);
public void browseAfter(String loginName, String ProductName);
public void browseAround(String loginName, String ProductName);
public void browseException(String loginName, String ProductName);
}
- 创建包com.ssm.service.impl,包中创建ProductServiceImpl实现ProductService,在类上添加@Component注解,自动创建ProductServiceImpl的Bean实例
package com.ssm.service.impl;
import org.springframework.stereotype.Component;
import com.ssm.service.ProductService;
@Component("productService")//自动注册Bean
public class ProductServiceImpl implements ProductService{
@Override
public void browseBefore(String loginName, String ProductName) {
System.out.println("执行browseBefore()");
}
@Override
public void browseException(String loginName, String ProductName) {
// TODO Auto-generated method stub
System.out.println("执行browseException()");
throw new RuntimeException("运行超时异常");
}
@Override
public void browseAfterReturn(String loginName, String ProductName) {
System.out.println("执行browseAfterReturn()");
}
@Override
public void browseAround(String loginName, String ProductName) {
System.out.println("执行browseAround()");
int i=100000000;
while(i>0) i--;
}
@Override
public void browseAfter(String loginName, String ProductName) {
System.out.println("执行browseAfter()");
}
}
- 创建com.ssm.aop包,包中创建日志通知类AllLogAdvice
package com.ssm.aop;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
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.springframework.stereotype.Component;
@Aspect//注册切面
@Component("allLogAdvice")//自动注册Bean
public class AllLogAdvice {
/*切入点表达式语法: execution([修饰符] 返回值类型 包名.类名.方法名(参数))
*下面的表达式意思是任意包及子包下的ProductService类的browseBefore()方法
*/
@Before("execution(* *..ProductService.browseBefore(String,String))")
public void myBeforeAdvice(JoinPoint joinPoint) {
List<Object> args = Arrays.asList(joinPoint.getArgs());
String logInfo = "前置通知: "
+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
+" "+args.get(0).toString()+" 浏览商品 "+args.get(1).toString();
System.out.println(logInfo);
}
@AfterReturning("execution(* *..ProductService.browseAfterReturn(String,String))")
public void myAfterReturnAdvice(JoinPoint joinPoint) {
List<Object> args = Arrays.asList(joinPoint.getArgs());
String logInfo = "后置通知: "
+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
+" "+args.get(0).toString()+" 浏览商品 "+args.get(1).toString();
System.out.println(logInfo);
}
@AfterThrowing(pointcut="execution(* *..ProductService.browseException(String,String))", throwing="e")
public void myThrowingAdvice(JoinPoint joinPoint, Exception e) {
//获取被调用方法的类名
String className = joinPoint.getTarget().getClass().getName();
//获取被调用的方法名
String methodName = joinPoint.getSignature().getName();
String logInfo = "异常通知: 执行"+className+"类的"+methodName+"方法发生异常";
System.out.println(logInfo);
}
@Around("execution(* *..ProductService.browseAround(String,String))")
public void myAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
long startTime = System.currentTimeMillis();
joinPoint.proceed();
long endTime = System.currentTimeMillis();
//获取被调用的方法名
String methodName = joinPoint.getSignature().getName();
String logInfo = "环绕通知: "+methodName+"方法调用前时间: "
+startTime+" ms,调用后时间: "+endTime+" ms";
System.out.println(logInfo);
}
@After("execution(* *..ProductService.browseAfter(String,String))")
public void myAfterAdvice(JoinPoint joinPoint) {
List<Object> args = Arrays.asList(joinPoint.get`在这里插入代码片`Args());
String logInfo = "最终通知: "
+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
+" "+args.get(0).toString()+" 浏览商品 "+args.get(1).toString();
System.out.println(logInfo);
}
}
- application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
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.ssm"/>
<!-- 打开@AspectJ切面的注解处理器 -->
<aop:aspectj-autoproxy/>
</beans>
- 创建com.ssm包,包中创建测试类TestAop
package com.ssm;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ssm.service.ProductService;
public class TestAop {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
ProductService p = (ProductService) context.getBean("productService");
p.browseBefore("张三", "java从入门到放弃");
p.browseAfterReturn("张三", "java从入门到放弃");
p.browseAround("张三", "java从入门到放弃");
p.browseAfter("张三", "java从入门到放弃");
p.browseException("张三", "java从入门到放弃");
}
}
运行结果:
说明:
- JoinPont参数joinPoint其实就是完整的切入点表达式。
以前置通知为例:
- 被增强的方法就是一个连接点,连接点的集合组成切入点,切入点表达式描述了连接点的集合。