springAop前后置通知最简单案例

springAop前后置通知最简单案例

仅仅针对于spring
案例分析:
该案例执行Demo类中的三个方法,分别输出Demo1,Demo2,Demo3
我们以Demo2为切点,分别执行前置通知和后置通知

1.首先导jar包
在这里插入图片描述

2.写applicationContext.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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <!-- 将Demo放入bean容器中 -->
        <bean id="demo" class="com.hym.bean.Demo"></bean>
        <!-- 将前置通知和后置通知也放入到bean容器中  id 自己任意取,后续引用就取id   ,class是全类名   -->
        <bean id ="myBefore" class="com.hym.advice.MyBeforeAdvice"></bean>
        <bean id ="myAfter" class="com.hym.advice.MyAfterAdvice"></bean>
        <aop:config>
        	<!-- 围绕的哪一个切点进行前后置通知  execution(* 全类名+方法名 )  这是固定写法    id 自己取名,后续引用就取id-->
        	<aop:pointcut expression="execution(* com.hym.bean.Demo.Demo2())" id="mypoint"/>
        	<!--  通知      根据advice-ref中的值 来区分是前置通知还是后置通知 。  值就是前后置通知的id  pointcut-ref 是切点的id-->
        	<aop:advisor advice-ref="myBefore" pointcut-ref="mypoint"/>
        	<aop:advisor advice-ref="myAfter" pointcut-ref="mypoint"/>
        </aop:config>
        <!-- r如果存在两个参数,name和id 那么用以下的写法 -->
        <!-- <aop:config>
        	<aop:pointcut expression="execution(* com.hym.bean.Demo.Demo2(String,int) and args(name,id)) " id=""/>
        </aop:config> -->
    
</beans>

3.项目架构
在这里插入图片描述
4.Demo类

package com.hym.bean;

public class Demo {
	public void Demo1() {
		System.out.println("Demo1");
	}
	public void Demo2() {
		System.out.println("Demo2");
	}
	public void Demo3() {
		System.out.println("Demo3");
	}

}

5.前后置通知
前置通知:

类中方法需要实现MethodBeforeAdvice

package com.hym.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyBeforeAdvice implements MethodBeforeAdvice{

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("执行前置通知");
		
	}
	

}

后置通知;
类中方法需要实现AfterReturningAdvice
该接口命名规范与前置通知有差异,需注意

package com.hym.advice;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class MyAfterAdvice implements AfterReturningAdvice{

	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		System.out.println("执行后置通知");
		
	}
	

}

最后测试类:

package com.hym.test;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hym.bean.Demo;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Demo demo = ac.getBean("demo",Demo.class);
		demo.Demo1();
		demo.Demo2();
		demo.Demo3();
		
	}

}

最终执行结果:
在这里插入图片描述

AOP:面向切面编程,
在执行Demo时,是纵向执行的,先Demo1,Demo2,Demo3.
但是我们以Demo2为切点,添加了前后置通知,这三个形成了一个横向的切面过程。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值