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()方法调用之前调用下面的方法。

  • 相关阅读:
    NodeJS NPM 镜像使用方法
    用for; while...do; do...while; 写出九九乘法表
    create-react-app创建的项目中registerServiceWorker.js文件的作用
    前端应该从哪些方面优化网站?
    JS基础整理面试题
    netcore实践:跨平台动态加载native组件
    iOS开发--Swift RAC响应式编程初探
    算法导论学习笔记 (页码:9 ~ 16)
    iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条
    iOS开发--面试
  • 原文地址:https://www.cnblogs.com/cnJun/p/3793948.html
Copyright © 2011-2022 走看看