zoukankan      html  css  js  c++  java
  • spring_AOP_annotation

     例子下载

    beans.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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <aop:aspectj-autoproxy />
    </beans>

      其中<context:annotation-config />为声明使用annotation部分,<context:component-scan base-package="com.bjsxt"/>会使得程序运行时进行对com.bjsxt包及子包的扫描操作,<aop:aspectj-autoproxy />对于AOP的annotation来说最为主要,标志着可以使用AOP的annotion来进行操作。

    Aspect

    package com.bjsxt.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {
        @Pointcut("execution(public * com.bjsxt.service..*.add(..))")
        public void myMethod(){};
        
        @Around("myMethod()")
        public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("method around start");
            pjp.proceed();
            System.out.println("method around end");
        }
        
        @Before("myMethod()")
        public void before() {
            System.out.println("method before");
        }
    }

    @Aspect注释标志着此类为一个切面类。

    @Pointcut作为切入点,@Pointcut("execution(public * com.bjsxt.service..*.add(..))")说明在调用com.bjsxt.service下面的包或其子包的add方法(任意参数)时调用下面的方法。

    @Around("myMethod()")说明在myMethod()方法调用前运行一次下面的方法,在myMethod()方法调用结束后再调用一次下面的方法。

    @Before("myMethod()")说明在myMethod()方法调用之前调用下面的方法。

  • 相关阅读:
    DockerPush
    DockerInstall
    DockerFile
    基于虚拟机实例/java程序线程的虚拟机内存分配
    Class文件结构及方法中的指令
    JAVA类型生命周期的开始阶段和使用阶段/以及创建对象的几种方式
    。。。。。毕业季
    PCA算法
    ffmpeg将图片转为视频
    Linux下使用bgslibrary的OpenCv库
  • 原文地址:https://www.cnblogs.com/cnJun/p/3793948.html
Copyright © 2011-2022 走看看