AOP的实现与配置

AOP的概念

AOP是面向切面的变成思想,是对AOP的补充。AOP采用一种称为“横切”的技术,将涉及多业务流程的通用功能抽取病单独封装,形成独立的切面,在合适的实际将这些切面横向切入到业务流程制定的位置中

AOP的增强类分类

1. 前置增强

实现方法:

  • 1、创建一个核心功能类或切入点
public class UserService {
    public int login(String name,String pwd){
        System.out.println("UserService.login");
        return 1;
    }
}
  • 2、创建前置增强类
public class MyMethodAdvice implements MethodBeforeAdvice{

    @Override
    public void before(Method method, Object[] objects, @Nullable Object o) throws Throwable {
        //获取目标对象的方法名
        System.out.println("目标对象的方法名"+method.getName());
        //获取目标对象参数列表的实参
        System.out.println("目标对象参数列表的第一个实参:"+objects[0]);
        //获取目标对象的类路径
        System.out.println("目标对象的类路径"+o.getClass());
        System.out.println("前置MyMethodAdvice.before");
    }
}
  • 3、在spring容器中创建前置增强对象
<bean id="MyMethodAdvice" class = "com.gxy.aop.MyMethodAdvice"></bean>
  • 4、配置aop:定义切入点,进行编织

配置大前提,有了前提才可使用相关aop标签

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
</beans>

第二部分:

<aop:config>
        <!--定义切入点-->
        <aop:pointcut id="myPointCut" expression="execution(* com.gxy.service.UserService.login(..))"></aop:pointcut>
        <!--编织 前置增强-->
        <!--<aop:advisor advice-ref="MyMethodAdvice" pointcut-ref="myPointCut"></aop:advisor>
</aop:config> 

注: id:自定义
expression:excution(* 核心功能的类路径.核心功能中定义的方法名(…))
advice-ref:与增强类中的id一致 pointcut-ref:与定义切入点的id一致

2. 后置增强

  • 1、创建一个核心功能类
public class UserService {
    public int login(String name,String pwd){
        System.out.println("UserService.login");
        return 1;
    }
}
  • 2、创建后置增强类
public class MyAfterAdvice implements AfterReturningAdvice{
    @Override
    public void afterReturning(@Nullable Object o, Method method, Object[] objects, @Nullable Object o1) throws Throwable {
        //获取目标对象的类路径
        System.out.println(o.getClass());
        //获取目标对象的方法名
        System.out.println(method.getName());
        //获取目标对象参数列表的实参
        System.out.println(objects[0]);
        //获取目标对象中方法的返回值类型
        System.out.println(o1.getClass());
        System.out.println("后置MyAfterAdvice.AfterReturningAdvice()");
    }
}
  • 3、在spring容器中创建后置增强对象
 <bean id="MyAfterAdvice" class="com.gxy.aop.MyAfterAdvice"></bean>
  • 4、配置aop:定义切入点,进行编织
<!--定义切入点-->
        <aop:pointcut id="myPointCut" expression="execution(* com.gxy.service.UserService.login(..))"></aop:pointcut>
<!--编织-->
<aop:advisor advice-ref="MyAfterAdvice" pointcut-ref="myPointCut"></aop:advisor>

3. 异常增强

  • 1、创建一个核心功能类
public class UserService {
    public int login(String name,String pwd){
        System.out.println("UserService.login");
        //制造异常
        int a = 1/0;
        return 1;
    }
}
  • 2、创建异常增强类
public class MyThrowAdvice implements ThrowsAdvice{
    public void afterThrowing(Method method,Object[] objects,Object target,Exception ex){
        System.out.println("MyThrowAdvice.afterThrowing()");
    }
}
  • 3、在spring容器中创建异常增强对象
 <!--增强类异常对象-->
    <bean id="MyThrowAdvice" class="com.gxy.aop.MyThrowAdvice"></bean>
  • 4、配置aop:定义切入点,进行编织
<!--定义切入点-->
        <aop:pointcut id="myPointCut" expression="execution(* com.gxy.service.UserService.login(..))"></aop:pointcut>
        <!--编织 异常增强-->
        <aop:advisor advice-ref="MyThrowAdvice" pointcut-ref="myPointCut"></aop:advisor>

4. 环绕增强

  • 1、创建一个核心功能类
public class UserService {
    public int login(String name,String pwd){
        System.out.println("UserService.login");
        return 1;
    }
}
  • 2、创建环绕增强类
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("MyInterceptor.invoke()");
        //目标对象的类路径
        System.out.println(methodInvocation.getThis().getClass());
        //目标对象的方法名
        System.out.println(methodInvocation.getMethod().getName());
        //目标对象的实参列表
        Object[] objects = methodInvocation.getArguments();
        for(int i=0;i<objects.length;i++){
            System.out.println(objects[i]);
        }
        //执行核心功能
        Object object =  methodInvocation.proceed();
        return object;
    }
  • 3、在spring容器中创建环绕增强对象
 <!--增强类环绕 对象-->
<bean id="MyIntercepor" class="com.gxy.aop.MyInterceptor"></bean>
  • 4、配置aop:定义切入点,进行编织
<!--定义切入点-->
        <aop:pointcut id="myPointCut" expression="execution(* com.gxy.service.UserService.login(..))"></aop:pointcut>
        <!--编织 异常增强-->
        <aop:advisor advice-ref="MyIntercepor" pointcut-ref="myPointCut"></aop:advisor>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值