(1)依赖包:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
(2)bean配置:4种切入通知:
before:在切入方法之前执行;
after-returning :在切入方法正常执行结束之后执行;
after-throwing:在切入方法执行出现异常时执行;
after :不管切入方法正常执行或者异常执行,都会执行;
注:4种通知,一次执行顶多执行其中三个,before和after都会执行,after-returning和after-throwing依据否切入方法执行是否有异常
<?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"
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
">
<bean id="accountService" class="service.impl.AccountServiceImpl"></bean>
<bean id="logger" class="utils.Logger"></bean>
<aop:config>
<aop:pointcut id="log" expression="execution(* service.impl.*..*(..))"></aop:pointcut>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="pringLogBefore" pointcut-ref="log"></aop:before>
<aop:after-returning method="pringLogAfterReturn" pointcut-ref="log"></aop:after-returning>
<aop:after-throwing method="pringLogAfterThrow" pointcut-ref="log"></aop:after-throwing>
<aop:after method="pringLogAfter" pointcut-ref="log"></aop:after>
</aop:aspect>
</aop:config>
</beans>
(3)环绕通知:
环绕通知是spring支持通过代码方式实现更加灵活的方式进行对切入方法进行aop控制
<?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"
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
">
<bean id="accountService" class="service.impl.AccountServiceImpl"></bean>
<bean id="logger" class="utils.Logger"></bean>
<aop:config>
<aop:pointcut id="log" expression="execution(* service.impl.*..*(..))"></aop:pointcut>
<aop:aspect id="logAdvice" ref="logger">
<!--<aop:before method="pringLogBefore" pointcut-ref="log"></aop:before>-->
<!--<aop:after-returning method="pringLogAfterReturn" pointcut-ref="log"></aop:after-returning>-->
<!--<aop:after-throwing method="pringLogAfterThrow" pointcut-ref="log"></aop:after-throwing>-->
<!--<aop:after method="pringLogAfter" pointcut-ref="log"></aop:after>-->
<aop:around method="printAround" pointcut-ref="log"></aop:around>
</aop:aspect>
</aop:config>
</beans>
通知方法:printAround
public Object printAround(ProceedingJoinPoint pjt){
Object ob = null;
try{
System.out.println("before");
Object[] args = pjt.getArgs();
ob = pjt.proceed(args);
System.out.println("afterReturn");
return ob;
}catch(Throwable t){
System.out.println("afterThrow");
}
finally {
System.out.println("after");
}
return ob;
}
(4)aop注解方式配置
(1)配置:
<?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/aop
http://www.springframework.org/schema/aop/spring-context.xsd
">
<!--配置spring容器扫描包-->
<context:component-scan base-package="service,utils"></context:component-scan>
<!--开启aop配置-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
(2)配置切面类:
package utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@Component("logger")
@Aspect //表示当前类为切面类
public class Logger {
// 配置切点表达式
@Pointcut("execution(* service.*..*(..))")
private void point1(){
}
@Before("point1()") //前置通知
public void pringLogBefore(){
System.out.println("before");
}
@AfterReturning("point1()") //后置通知
public void pringLogAfterReturn(){
System.out.println(" public void pringLogAfterReturn()");
}
@AfterThrowing("point1()") //异常通知
public void pringLogAfterThrow(){
System.out.println(" public void pringLogAfterThrow()");
}
@After("point1()") //最终通知
public void pringLogAfter(){
System.out.println(" pringLogAfter");
}
@Around("point1()") // 环绕通知
public Object printAround(ProceedingJoinPoint pjt){
Object ob = null;
try{
System.out.println("before");
Object[] args = pjt.getArgs();
ob = pjt.proceed(args);
System.out.println("afterReturn");
return ob;
}catch(Throwable t){
System.out.println("afterThrow");
}
finally {
System.out.println("after");
}
return ob;
}
}