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

应用:数据校验

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
    }
}

结果:

spring中aop中的before_spring中aop中的before

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

第一种方式:

在通知方法上传入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
    }
}

结果:

spring中aop中的before_容器_02

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

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

public class UserServiceImpl implements UserService {
@Override
public 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(* *..*(..))&&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中aop中的before_spring_03

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

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

前置通知异常信息

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