Spring_前置通知before的用法

需要更多教程,微信扫码即可
  

前置通知:原始方法执行前执行,如果通知中抛出异常,阻止原始方法运行

应用:数据校验

aop:before

  • 名称:aop:before

  • 类型:标签

  • 归属:aop:aspect标签

  • 作用:设置前置通知

  • 格式:

    <aop:aspect ref="adviceId">
       <aop:before method="methodName" pointcut="……"/>
    </aop:aspect>
  • 说明:一个aop:aspect标签中可以配置多个aop:before标签

  • 基本属性:

    • method :在通知类中设置当前通知类别对应的方法

    • pointcut :设置当前通知对应的切入点表达式,与pointcut-ref属性冲突

    • pointcut-ref :设置当前通知对应的切入点id,与pointcut属性冲突

前置配置

1.创建通知类

public class AOPAdvice {    public void before(){        System.out.println("我是前置通知");    }}

创建UserServiceImpl类​​​​​​​

public class UserServiceImpl implements UserService {    @Override    public void add(String name) {        System.out.println("add()执行了,name="+name);    }}

2.修改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        https://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        https://www.springframework.org/schema/aop/spring-aop.xsd">   <!--将所有进行AOP操作的资源加载到IoC容器中-->    <bean id="aopadvice" class="com.javaxxf.aop.AOPAdvice" />    <!--aop配置-->    <aop:config>        <!--配置切入点-->        <aop:pointcut id="pt" expression="execution(* *..*(..))"/>        <!--配置切面-->        <aop:aspect ref="aopadvice">            <!--通知与切入点之间的关系-->            <aop:before method="before" pointcut-ref="pt"/>        </aop:aspect>    </aop:config>    <!--配置service bean-->    <bean id="userService" class="com.javaxxf.service.Impl.UserServiceImpl"/></beans>

6.运行程序​​​​​​​

public class UserController {    public static void main(String[] args) {        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");        UserService userService= (UserService) ac.getBean("userService");        userService.add("我是java");       //根据id查询user    }}

结果:

图片

前置通知获取执行方法的参数数据

第一种方式:

在通知方法上传入JoinPoint jp参数,通过getArgs()方法获取参数数组,然后可以从数组里面可以获取到执行方法的参数(所有的通知均可以用这种方式获取执行方法的数据)​​​​​​​

public class AOPAdvice {    public void before(JoinPoint jp){        Object[] args = jp.getArgs();        System.out.println("我是前置通知..."+args[0]);    }}

测试​​​​​​​

public class UserController {    public static void main(String[] args) {        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");        UserService userService= (UserService) ac.getBean("userService");        userService.add("我是java");        //根据id查询user    }}

结果:

图片

第二种方式:通过配置文件获取

1、首先UserServiceImpl的add(String name)是String​​​​​​​

public class UserServiceImpl implements UserService {@Overridepublic void add(String name) {        System.out.println("add()执行了,name="+name);    }}

2、通知类的方法也要加上一个string的参数,这个名字可以随意定义​​​​​​​

public class AOPAdvice {    public void before(String xxx){        System.out.println("我是前置通知..."+xxx);    }}

3、修改配置文件​​​​​​​

<?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">   <!--将所有进行AOP操作的资源加载到IoC容器中-->    <bean id="aopadvice" class="com.javaxxf.aop.AOPAdvice" />    <!--aop配置-->    <aop:config>        <!--配置切面-->        <aop:aspect ref="aopadvice">            <!--通知与切入点之间的关系-->            <aop:before method="before" pointcut="execution(* *..*(..))&amp;&amp;args(xxx)"/>        </aop:aspect>    </aop:config>    <!--配置service bean-->    <bean id="userService"  class="com.javaxxf.service.Impl.UserServiceImpl"/></beans>

补充:

1、使用&&需要转义

2、args(xxx) 括号里面的值需要与通知类的方法的参数名一致

3、当你的执行方法中有多个同类型的参数,你可以通过arg-names="xxx1,xxx2"修改参数的赋值顺序,这里我就不演示了,如果有不了解的可以私聊小编

测试:​​​​​​​

public class UserController {    public static void main(String[] args) {        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");        UserService userService= (UserService) ac.getBean("userService");        userService.add("我是学java");        //根据id查询user    }}

结果:

图片

前置通知获取执行方法的返回值

获取不了返回值,因为前置通知是在执行方法运行前执行。

前置通知异常信息

获取不了异常信息,因为前置通知是在执行方法运行前执行。

 Spring学习交流、资料领取。扫码即可哦!

文末福利

需要更多教程,微信扫码即可
   

                   别忘了扫码领取资料哦                 
  【高清Java学习线路图】和【全套学习视频及相关资料】 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值