Spring AOP
概念



5种方法

使用前提

代码例子
假设我们有增删查改4个方法,要在不改变源码的前提下在运行前后提供日志功能
定义接口
package service;
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
package service;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("add---");
}
public void delete() {
System.out.println("delete---");
}
public void update() {
System.out.println("update---");
}
public void query() {
System.out.println("query---");
}
}
实现在方法前的代理
package log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method 要使用的方法
//objects 参数
//target 目标对象
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
实现方法执行后的代理
package log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"返回值为"+returnValue);
}
}
配置文件:
<?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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"
>
<bean id="userService" class="service.UserServiceImpl"/>
<bean id="log" class="log.Log"/>
<bean id="afterLog" class="log.AfterLog"/>
<!--配置AOP -->
<aop:config>
<!--切入点 execution()要执行的位置 修饰词 返回值 类名 方法名 参数 (..)代表任意参数-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
<context:annotation-config/>
</beans>
主程序:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
通过自定义类来实现
上面方法是通过实现自带的接口实现AOP,我们也可以自定义类来实现
增加自定义类
package diy;
public class DiyPointClass {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
修改配置文件
<?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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"
>
<bean id="userService" class="service.UserServiceImpl"/>
<bean id="log" class="log.Log"/>
<bean id="afterLog" class="log.AfterLog"/>
<!--配置AOP -->
<bean id="diy" class="diy.DiyPointClass"/>
<aop:config>
<!--自定义切面 在AOP里切面通过类实现 所以我们自定义类选择这种方式-->
<aop:aspect ref="diy">
<!--设置要进行切的地方-->
<aop:pointcut id="point" expression="execution(* service.UserServiceImpl.*(..))"/>
<!--在point执行方法前调用diy的before函数-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
其他地方不改变即可
环绕通知
AOP除了前置通知和后置通知还有环绕通知
环绕通知的格式
1.public
2.必须有一个返回值,推荐object
3.方法有固定参数:ProceedingJoinPoint

package diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标记这个类是一个切面
public class AnnotationPointCut {
@Around("execution(* service.UserServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
//Object[] args = pjp.getArgs();
//获取参数信息,通过信息可以实现决定要不要调用pjp.proceed()
Object result=null;
System.out.println("环绕方法执行前--");
result=pjp.proceed();//等价于method.invoke
//在这之后可以继续对result进行修改,实现对结果功能的附加
System.out.println("环绕方法执行后---");
return result;
}
}

浙公网安备 33010602011771号