对于AOP,我们明确的知道,是Spring的核心之一,这里,博主分享Spring实现AOP的三种方式。
准备环境如下:
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
- 准备Spring的配置文件,applicationContext.xml,一定要引入AOP的约束
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
beans>
准备一个模拟业务层service,其中包括UserService接口和UserServiceImpl实现类:
UserService接口:
package com.ara.service;
public interface UserService {
void add();
void delete();
void update();
void query();
}
UserServiceImpl实现类:
package com.ara.service;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("修改了一个用户");
}
public void query() {
System.out.println("查询了一个用户");
}
}
将业务实现的bean添加到Spring配置中:
<bean id="userService" class="com.ara.service.UserServiceImpl"/>
测试代码:
@Test
public void springTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
测试代码一直保持不变,就更能体现出AOP的思想了。
这样直接调用的结果为:

现在的需求是:在不改动上述代码的前提下,我们调用其中的方法会打印出对应的切入信息。
1. 方式一:使用原生Spring API接口
编写切入类:
BeforeLog:
package com.ara.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置切入");
System.out.println(target.getClass().getName() + "-" + method.getName());
}
}
AfterLog:
package com.ara.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置切入");
System.out.println(method.getName() + ":" + returnValue);
}
}
然后我们在Spring配置文件中配置切面:
<bean id="beforeLog" class="com.ara.log.BeforeLog"/>
<bean id="afterLog" class="com.ara.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.ara.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointCut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointCut"/>
aop:config>
配置好后,我们再对测试代码进行测试,发现结果如下:

发现得到了我们想要的结果。
2.方式二:自定义类
编写切入代码:
package com.ara.diy;
public class DiyPointCut {
public void before(){
System.out.println("========before()========");
}
public void after(){
System.out.println("========after()========");
}
}
在Spring配置文件中配置:
<bean id="diy" class="com.ara.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.ara.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
aop:aspect>
aop:config>
测试结果如下:

同样也得到了我们想要的结果
3.方式三:使用注解方式
编写切入类:
package com.ara.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.ara.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("========before()========");
}
@After("execution(* com.ara.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("========after()========");
}
@Around("execution(* com.ara.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("========around-before()========");
System.out.println(point.getSignature());
Object proceed = point.proceed();
System.out.println("========around-after()========");
System.out.println(proceed);
}
}
在Spring配置文件中添加:
<bean id="annotationPointCut" class="com.ara.diy.AnnotationPointCut" />
<aop:aspectj-autoproxy/>
然后再对代码进行测试,结果如下:

这样我们也能得到对应的效果。
至于我们需要选择哪种方式实现,则根据实际需求选择即可。