zoukankan      html  css  js  c++  java
  • Spring AOP

    1、配置

      导入AspectJ Weaver包;

      xml配置aop相关;

    <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:aspectj-autoproxy />

    2、属性说明

        aspect:切面  -  横切关注点被模块化的特殊对象;即,它是一个类

        advice:通知  -  切面必须要完成的工作。即,他是类中的一个方法

        target:目标  -  被通知对象

        proxy:代理  -  向目标对象应用通知之后创建的对象

        pointcut:切入点  -  切面通知执行的“地点”的定义

        jointPoint:连接点  -  s与切入点匹配的执行点

    3、实现

      3.1 Spring API接口实现

        xml配置

       <!--方式一:xml配置实现-->
        <aop:config>
            <!--切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.doubleh.service.UserServiceImpl.*(..))" />
            <!--通知-->
            <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut" />
            <aop:advisor advice-ref="logAfter" pointcut-ref="pointcut" />
        </aop:config>

        实现通知接口重写方法;

    import org.springframework.aop.MethodBeforeAdvice;
    import java.lang.reflect.Method;
    
    public class logBefore implements MethodBeforeAdvice {
    
        public void before(Method method, Object[] args, Object target) throws Throwable {
            System.out.println(target.getClass().getName() +"类,执行了"+method.getName()+"方法");
        }
    }

        测试;

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    //        接口接收调用功能
            UserService userServiceImpl = applicationContext.getBean("userService", UserService.class);
            userServiceImpl.create();

      3.2 注解实现

        首先需要在xml配置自动代理设置。

       <aop:aspectj-autoproxy />

        然后编写切面类,方法即通知定义。

    @Component
    @Aspect
    public class aopAspect {
    
        @Before("execution(* com.doubleh.service.UserServiceImpl.*(..))")
        public void before(){
            System.out.println("这是开始");
        }
    
        @After("execution(* com.doubleh.service.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("这是结束");
        }
    
        @Around("execution(* com.doubleh.service.UserServiceImpl.*(..))")
        public void around(ProceedingJoinPoint pj) throws Throwable {
            System.out.println("环绕前");
            Object proceed = pj.proceed();
           System.out.println(pj.getSignature());
            System.out.println("环绕后");
        }
    }

      3.3 自定义类实现

        类似第二种,知识在xml去切面、切入点和通知方法。

    <!--方式三:自定义类实现-->
        <aop:config>
            <aop:aspect  ref="aopAspect">
              <aop:pointcut id="pointcut" expression="execution(* com.doubleh.service.UserServiceImpl.*(..))"></aop:pointcut>
                <aop:before method="before" pointcut-ref="pointcut" />
                <aop:after method="after" pointcut-ref="pointcut" />
            </aop:aspect>
        </aop:config>
  • 相关阅读:
    [C]recursion递归计算阶乘
    [Python]reduce function & lambda function & factorial
    [C/JAVA] ceil, floor
    OC项目调用C++
    Xcode 代码注释
    百度云加速器
    UITableView和MJReFresh结合使用问题记录
    OC 类的load方法
    JLRoutes笔记
    推送通知项目记录
  • 原文地址:https://www.cnblogs.com/xp2h/p/12378622.html
Copyright © 2011-2022 走看看