zoukankan      html  css  js  c++  java
  • AOP

    在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点"

    例如定义切入点表达式 execution(* com.sample.service.impl..*.*(..))

    execution()是最常用的切点函数,其语法如下所示:

     整个表达式可以分为五个部分:

     1、execution(): 表达式主体。

     2、第一个*号:表示返回类型,*号表示所有的类型。

     3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。

     4、第二个*号:表示类名,*号表示所有的类。

     5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

    定义AOP:
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    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;
    
    @Component
    @Aspect
    public class MyAop 
    {
        @Pointcut("execution(* *.test(..))")
        public void test()
        {
            
        }
        
        @Before("test()")
        public void beforeTest()
        {
            System.out.println("this is before test");
        }
        
        @After("test()")
        public void afterTest()
        {
            System.out.println("this is after test");
        }
        
        @Around("test()")
        public Object arount(ProceedingJoinPoint p)
        {
            System.out.println("before.....1");
            Object o = null;
            try
            {
                o = p.proceed();
            }
            catch(Throwable e)
            {
                
            }
            System.out.println("after...1");
            return o;
        }
    
    }
    定义调用Aop的Bean
    @Component("aopBean")
    public class AopBean 
    {
        public void test()
        {
            System.out.println("this is my test message.....");
        }
    
    }


    spring2.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <aop:aspectj-autoproxy /> <context:annotation-config /> <context:component-scan base-package="com.donghua.aop"/> </beans>

    测试:

    ApplicationContext bf = new ClassPathXmlApplicationContext("spring2.xml");
    AopBean aop = (AopBean) bf.getBean("aopBean");
    aop.test();

    输出:

    before.....1
    this is before test
    this is my test message.....
    after...1
    this is after test

    应用AOP后返回的是代理对象

  • 相关阅读:
    《DSP using MATLAB》Problem 6.17
    一些老物件
    《DSP using MATLAB》Problem 6.16
    《DSP using MATLAB》Problem 6.15
    《DSP using MATLAB》Problem 6.14
    《DSP using MATLAB》Problem 6.13
    《DSP using MATLAB》Problem 6.12
    《DSP using MATLAB》Problem 6.11
    P1414 又是毕业季II
    Trie树
  • 原文地址:https://www.cnblogs.com/daxiong225/p/9608900.html
Copyright © 2011-2022 走看看