除了AspectJ,Spring支持原生方式实现AOP
引入依赖
<!-- AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.18</version>
</dependency>
编写通知类
package com.neu.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
/**
* @Author yqq
* @Date 2022/04/22 13:09
* @Version 1.0
* Spring原生Aop的通知类
*/
public class SpringAop implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice, MethodInterceptor {
/**
* 环绕通知
* @param invocation 目标方法
* @return
* @throws Throwable
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("环绕前");
Object proceed = invocation.proceed();//目标方法
System.out.println("环绕后");
return proceed;
}
/**
* 后置通知
* @param returnValue 目标方法的返回值
* @param method 目标方法
* @param args 目标方法的参数列表
* @param target 目标对象
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置通知");
}
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知");
}
/**
* 异常通知
* @param ex 异常对象
*/
public void afterThrowing(Exception ex){
System.out.println("发生异常");
}
}
Spring原生方式实现AOP时,只支持四种通知类型:
核心类
package com.neu.dao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
/**
* @Author yqq
* @Date 2022/04/14 23:51
* @Version 1.0
*/
public class UserDao {
public void add(){
System.out.println("用户新增");
}
public void del(){
System.out.println("用户删除");
}
public void update(){
int i = 1/0;
System.out.println("用户修改");
}
}
编写配置类
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.neu.dao"></context:component-scan>
<!--通知对象-->
<bean id="springAop" class="com.neu.advice.SpringAop"></bean>
<!--配置代理对象-->
<bean id="userDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标对象-->
<property name="target" ref="userDao"></property>
<!--配置通知-->
<property name="interceptorNames">
<list>
<value>springAop</value>
</list>
</property>
<!--代理对象的生成方式 true:使用CGLib false:使用原生JDK生成-->
<property name="proxyTargetClass" value="true"></property>
</bean>
</beans>
编写测试类
public void testAdd(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
UserDao userDao = (UserDao) ac.getBean("userDaoProxy");
userDao.add();
}