一、导入Spring的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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置Sping 要扫描的包-->
<context:component-scan base-package="com"/>
<!--配置自动完成创建代理织入切面的工作-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
注解的使用
package com.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("logUtil")
@Aspect //配置织入@Aspectj切面
public class ServiceInfo {
@Pointcut("execution(* com.service.impl.*.*(..))")
public void getcut(){}
@Before("getcut()")
public void before(){
System.out.println("before");
}
@After("getcut()")
public void after(){
System.out.println("after");
}
@AfterThrowing("getcut()")
public void error(){
System.out.println("error");
}
@AfterReturning("getcut()")
public void normal(){
System.out.println("normal");
}
// @Around("getcut()") // 环绕写法
public void aroundPingLog(ProceedingJoinPoint joinPoint){
Object rValue =null;
try {
Object [] args=joinPoint.getArgs();
System.out.println("前·····");
rValue=joinPoint.proceed(args);/*业务层,切入点方法*/
System.out.println("正常后·····");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("异常后·····");
}finally {
System.out.println("结束·····");
}
}
}