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

  • 相关阅读:
    温故而知新-错误和异常处理
    温故而知新-面向对象的PHP
    Django框架之模板语法【转载】
    django2.0实现数据详情页展示的流程
    django2.0表的ORM字段类型和展示
    Fatal error: Cannot use object of type PHPExcel_RichText as array
    django2.0数据展示流程
    django2.0模板相关设置
    django2.0新增功能流程
    django2.0设置默认访问路由
  • 原文地址:https://www.cnblogs.com/cnJun/p/3793948.html
Copyright © 2011-2022 走看看