AOP
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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.ljm.service.UserService_"/> <bean id="log" class="com.ljm.log.Log"/> <bean id="afterLog" class="com.ljm.log.Afterlog"/> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.ljm.service.UserService_.*(..))"/> <aop:pointcut id="pointcut1" expression="execution(* com.ljm.service.UserService_.add(..))"/> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut1"/> aop:config> beans>
接口(略)
实现类(略)
前置
public class Log implements MethodBeforeAdvice { @Override //method 需要执行目标对象的方法 //objects 相关参数 ==args //o 目标对象 ==target //自动调用 public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("前置 "+o.getClass().getName()+" 的 "+method.getName()+" 方法正在运行 "); } }
后置
public class Afterlog implements AfterReturningAdvice { @Override //returenValue 返回值 //method 需要执行目标对象的方法 //objects 相关参数 ==args //o 目标对象 ==target public void afterReturning(Object returenValue, Method method, Object[] objects, Object o1) throws Throwable { System.out.println(" after 执行了"+method.getName()+"方法 返回的结果为"+returenValue); } }
测试
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //动态代理的是接口 UserService是接口 UserService userService = (UserService)context.getBean("userService"); userService.add(); userService.delete(); }
方法2:自定义类(视频推荐)
相对简单,但功能有限
public class DiyPointCut { public void before(){ System.out.println("=====前面执行==="); } public void after(){ System.out.println("=====后面执行==="); } }
XML相关
<bean id="diy" class="com.ljm.diy.DiyPointCut"/> <aop:config> <aop:aspect ref="diy"> <aop:pointcut id="point" expression="execution(* com.ljm.service.UserService_.*(..))"/> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> aop:aspect> aop:config>
方法3:使用注解实现(调试代码不变)
在pom中导入依赖
<dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.9.4version> <scope>runtimescope> dependency> <dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjrtartifactId> <version>1.9.4version> dependency>
自定义相关类
@Aspect public class AnotationPointCut { @Before("execution(* com.ljm.service.UserService_.*(..))") public void before(){ System.out.println("方法执行前-注解实现,Before"); } @After("execution(* com.ljm.service.UserService_.*(..))") public void after(){ System.out.println("方法执行后-注解实现,After"); } //在环绕增强中,可以给个参数,代表获取处理的点 @Around("execution(* com.ljm.service.UserService_.*(..))") public void around(ProceedingJoinPoint jp){ System.out.println("环绕前"); // jp.getSignature(); System.out.println("签名=执行方法="+jp.getSignature()); try { Object proceed = jp.proceed(); //执行方法 } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("环绕后"); } }
配置XML中
<bean id="diy01" class="com.ljm.diy.AnotationPointCut"/>
<aop:aspectj-autoproxy proxy-target-class="false"/>
执行顺序