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

  • 相关阅读:
    mysql关联查询
    MySQL数据库面试题(转发来自https://thinkwon.blog.csdn.net/article/details/104778621)
    iview + vue + thinphp5.1 源码
    <!--标签嵌套规则-->
    PHP的基本变量检测方法
    PHP的八种变量
    php变量命名规范
    C++11新特性-常用
    算法设计-DP常见问题及解题技巧
    Web开发-概述
  • 原文地址:https://www.cnblogs.com/cnJun/p/3793948.html
Copyright © 2011-2022 走看看