对于 AOP 这种编程思想,很多框架都进行了实现。Spring 就是其中之一,可以完成面向切面编程。
AspectJ 也实现了 AOP 的功能,且其实现方式更为简捷,使用更为方便,而且还支持注解式开发。所以,Spring 又将 AspectJ 的对于 AOP 的实现也引入到了自己的框架中。
我们开发的时候一般都会使用AspectJ的实现方式。
通知类型
AspectJ 中常用的通知有五种类型:
(1)前置通知
(2)后置通知
(3)环绕通知
(4)异常通知
(5)最终通知
切入点表达式
AspectJ 定义了专门的表达式用于指定切入点。表达式的原型是:
execution(modifiers-pattern? ret-type-pattern
declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?)
说明:
execution(访问权限 方法返回值 方法声明(参数) 异常类型)
访问权限和异常类型是可以省略的。
下面说AspectJ的使用需要的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
jar包版本不对的话可能会有有类找不到异常
spring中使用aop
1:创建目标接口和实现类
2:定义切面类
在切面类中指定切入点、指定切入的时机和切入的内容
package com.dongmu.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspectJ {
@Before(value = "execution(* *(..))")
public void before(){
System.out.println("在目标方法执行之前执行的方法");
}
@After(value = "execution(* *(..))")
public void after(){
System.out.println("在目标方法执行之后执行的方法。");
}
}
3:写配置文件
将切面类和目标类注册到bean中。
<?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-3.2.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="goal" class="com.dongmu.target.Goal"/>
<bean id="MyAspectJ" class="com.dongmu.aop.MyAspectJ"/>
<context:component-scan base-package="com.dongmu.aop"/>
<aop:aspectj-autoproxy/>
</beans>
下面还是再介绍一下SpringAOP的实现方法吧
1:加入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
2:写增强的方法
package com.dongmu.aop;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class after implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("目标方法执行之后的方法!");
}
}
package com.dongmu.aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class before implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("目标方法执行之前的方法!");
}
}
3:写目标接口和实现类
package com.dongmu.goal;
public interface inter {
public void goal1();
}
package com.dongmu.goal;
public class sp1 implements inter{
public void goal1(){
System.out.println("目标方法执行了!");
}
}
4:配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="goal1" class="com.dongmu.goal.sp1"/>
<bean id="after" class="com.dongmu.aop.after"/>
<bean id="before" class="com.dongmu.aop.before"/>
<aop:config>
<aop:pointcut id="point1" expression="execution(* com.dongmu.goal.*.*(..) )"/>
<aop:advisor advice-ref="after" pointcut-ref="point1"/>
<aop:advisor advice-ref="before" pointcut-ref="point1"/>
</aop:config>
</beans>
5:测试
第二种方式其实还有一种方式就是自定义切入的类,就是不继承其他的类。
这个时候这么写:div是我们自定义的类,before是里面的方法
<bean id="divbefore" class="com.dongmu.aop.div"/>
<aop:config >
<aop:aspect ref="divbefore">
<aop:pointcut id="point2" expression="execution(* com.dongmu.goal.*.*(..) )"/>
<aop:before method="before" pointcut-ref="point2"/>
</aop:aspect>
</aop:config>
其实步骤基本上都是一样的。