1.AOP实现的原生方法spring的API接口
动态代理类似
SpringAPI接口实现类
public class BeforeLog implements MethodBeforeAdvice {
/**
*
* @param method 方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+"方法");
}
}
spring配置文件的编写
<?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到spring容器中-->
<bean id="UserImpl" class="service.UserImpl"/>
<bean id="BeforeLog" class="log.BeforeLog"/>
<aop:config>
<!-- 添加切入点 execution(* service.UserImpl.*(..))表示切入到这个类的所有方法-->
<aop:pointcut id="pointcut" expression="execution(* service.UserImpl.*(..))"/>
<!-- 设置切入的日志类 pointcut-ref切入到哪里 -->
<aop:advisor advice-ref="BeforeLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
接口和接口实现类的编写
package service;
/**
* @author:LeeGaki
* @date:2022/1/13
*/
public class UserImpl implements User{
@Override
public void add() {
System.out.println("add");
}
@Override
public void sel() {
System.out.println("sel");
}
@Override
public void del() {
System.out.println("del");
}
@Override
public void upd() {
System.out.println("upd");
}
}
测试类的编写
@Test
public void log(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取的返回值要是接口,获取的bean应该是实现类
User userImpl = context.getBean("UserImpl", User.class);
userImpl.add();
}
2.AOP实现的第二种方法
自定义类(切面)的实现
配置文件中aop的配置
<aop:config>
<!-- 创建切入点 第一个*代表返回值类型 后面表示切入到这个类的所有方法-->
<aop:pointcut id="point" expression="execution(* service.UserImpl.*(..))"/>
<!-- ref后是自定义切面 也就是自定义的类-->
<aop:aspect ref="DiyLog">
<!-- 切入到哪个方法的前还是后-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
自定义类(切面)
package log;
/**
* @author:LeeGaki
* @date:2022/1/13
*/
public class DiyLog {
public void before(){
System.out.println("====执行前========");
}
public void after(){
System.out.println("====执行后========");
}
}
测试类
@Test
public void log(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取的返回值要是接口,获取的bean应该是实现类
User userImpl = context.getBean("UserImpl", User.class);
userImpl.add();
}
3.注解式实现aop
首先打开aop注解支持在配置文件中加入
<aop:aspectj-autoproxy/>
编写自定义类 添加注解
package log;
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;
/**
* @author:LeeGaki
* @date:2022/1/13
*/
@Aspect
public class DiyLog {
@Before("execution(* service.UserImpl.*(..))")
public void before(){
System.out.println("====执行前========");
}
@After("execution(* service.UserImpl.*(..))")
public void after(){
System.out.println("====执行后========");
}
@Around("execution(* service.UserImpl.*(..))")
public void around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕前");
Object proceed = pj.proceed();
System.out.println("环绕后");
}
}
前后环绕都存在的时候实现的顺序是
环绕前
====执行前========
add
====执行后========
环绕后