zoukankan      html  css  js  c++  java
  • Spring AspectJ基于注解的AOP实现

    对于AOP这种编程思想,很多框架都进行了实现。Spring就是其中之一,可以完成面向切面编程。然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发。所以,Spring又将AspectJ对于AOP的实现也引入到了自己的框架中。
         在Spring中使用AOP开发时,一般使用AspectJ的实现方式。

    Spring的经典AOP配置方案
      01.使用的是Aspectj第三方框架,实现了AOP思想
      02.注解配置的AOP
      03.纯POJO <aop:config>

    切入点表达式
    execution(【modifiers-pattern?】 访问修饰符
    ret-type-pattern 返回值类型
    【declaring-type-pattern?】 全限定性类名
    name-pattern(param-pattern) 方法名(参数名)
    【throws-pattern?】) 抛出异常类型

    切入点表达式要匹配的对象就是目标方法的方法名。所以,execution表达式中明显就是方法的签名。注意:表达式中加[]的部分表示可省略部分,各部分间用空格分开。在其中可以使用以下符号:
    符号       意义
    *      0至多个任意字符
    ..     用在方法参数中,表示任意多个参数
       

           用在包名后,表示当前包及其子包路径
    +    用在类名后,表示当前类及其子类
           用在接口后,表示当前接口及其实现类
    案例:
    execution(public * *(..)) 指定切入点为:任意公共方法
    execution(* set*(..)) 指定切入点为:任何一个以"set"开始的方法

    引入jar包
       com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar  依赖包里
       spring-aspects-4.2.0.RELEASE.jar


      引入aop约束
       xmlns:aop="http://www.springframework.org/schema/aop"

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd


    ISomeService.java

    package cn.service;
    /**
     * 接口
     * @author Happy
     *
     */
    public interface ISomeService {
        public void list();
    }
    View Code

    SomeServiceImpl.java

    package cn.service;
    
    public class SomeServiceImpl implements ISomeService {
    
        public void list() {
            System.out.println("SomeServiceImpl.list()");
            
        }
    
        
    
    }
    View Code

    MyAspect.java

    package cn.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    
    @Aspect
    public class MyAspect {
        //前置通知
        @Before(value = "execution(* *..service.*.*(..))")
        public void MyBeforeAdvice(){
            System.out.println("====before");
        }
    
        
        //后置通知
            @AfterReturning(value="execution(* *..service.*.*(..))")
            public void myAfterReturning(){
                System.out.println("这是后置增强");
            }
            
            //环绕增强
            @Around(value="execution(* *..service.*.*(..))")
            public void myAround(ProceedingJoinPoint pjp){
                System.out.println("这是环绕前置增强");
                try {
                    pjp.proceed();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                System.out.println("这是环绕后置增强");
            }
            
            //异常增强
            @AfterThrowing(value="execution(* *..service.*.*(..))")
            public void myAfterThrowing(){
                System.out.println("这是异常增强");
            }
            
            //最终增强
            @After(value="execution(* *..service.*.*(..))")
            public void myAfter(){
                System.out.println("这是最终增强");
            }
    }
    View Code

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            ">
        <!-- 01.目标对象 -->
        <bean id="someService" class="cn.service.SomeServiceImpl"></bean>
        
        <!-- 02.切面:通知 -->
        <bean id="beforeAdvice" class="cn.aop.MyAspect"></bean>
        
        <aop:aspectj-autoproxy/>
        
    </beans>
    View Code

    MyTest.java

    package cn.staticproxy;
    
    
    
    import org.junit.Test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.service.ISomeService;
    
    public class MyTest {
        @Test
        public void aspectTest(){
            
            //我要一个学生对象
                    ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/staticproxy/applicationContext.xml");
                    ISomeService service=(ISomeService)ctx.getBean("someService");
                    service.list();
        }
    }
    View Code

    展示效果:

  • 相关阅读:
    InjectAPC全部项目(Win32和Win64位)
    Codeforces Round #377 (Div. 2)
    Codeforces Canada Cup 2016
    UVa 1395 (最小生成树)
    空间表SpaceList
    线程中的临界区的应用
    【题解】狼和羊-C++
    【基础算法-树状数组】入门-C++
    【题解】在你窗外闪耀的星星-C++
    【题解】[NOIP模拟题]我要的幸福-C++
  • 原文地址:https://www.cnblogs.com/jingpeipei/p/6008821.html
Copyright © 2011-2022 走看看