zoukankan      html  css  js  c++  java
  • spring aop的两种写法aspect和advisor

    本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html

    首先看个例子,如下

    接口代码:

    package com.lei.demo.aop.schema;
    
    public interface IHello {
        public void sayHello();
    }

    接口实现:

    package com.lei.demo.aop.schema;
    
    public class HelloService implements IHello {
    
        public void sayHello() {
            System.out.println("-----Hello World!-----");
        }
    
    }

    接下来我们要实现AOP,即调用sayHello方法时切入通知。

    1.      第一种方法aop:config中配置aop:pointcut和aop:aspect

    定义一个切面支持类HelloAspect.java

    package com.lei.demo.aop.schema;
    
    /*
     * 切面支持类
     */
    public class HelloAspect {
    
        //前置通知  
        public void beforeAdvice() {  
            System.out.println("===========before advice");  
        }
        
        //后置最终通知  
        public void afterFinallyAdvice() {  
            System.out.println("===========after finally advice");
        }
    }

     

    Xml配置:Spring-AOP-Schema.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:aop="http://www.springframework.org/schema/aop"  
            xsi:schemaLocation="  
               http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
               http://www.springframework.org/schema/aop  
               http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
               
    
        <!-- 目标类 -->
        <bean id="helloService" class="com.lei.demo.aop.schema.HelloService" />
        
        <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" />
        
        
        <!-- 配置切面 -->
        <!-- aop:advisor,是有顺序的,必须放在aop:pointcut之后 -->
        <aop:config>
            <aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
            
            <aop:aspect ref="helloAspect">
                <!—以下使用了两种方法定义切入点  pointcut-ref和pointcut-->
                <aop:before pointcut-ref="helloPointcut" method="beforeAdvice" />
                <aop:after pointcut="execution(* com.lei.demo.aop.schema..*.*(..))" method="afterFinallyAdvice" />
            </aop:aspect>
            
            
        </aop:config>
    </beans>

    以上配置中method="beforeAdvice"method="afterFinallyAdvice"对应helloAspect中的方法

    测试,App.java

    package com.lei.demo.aop.schema;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
    
        private static ApplicationContext ctx;
    
        public static void main(String[] args) {
            
            ctx = new ClassPathXmlApplicationContext("Spring-AOP-Schema.xml");
            
            IHello helloService = ctx.getBean("helloService",IHello.class);
            
            helloService.sayHello();
        }
    
    }

    运行App.java,结果如下:

    ===========before advice

    -----Hello World!-----

    ===========after finally advice

    2.      第二种方法aop:config中配置aop:pointcut和aop:advisor

    实现org.aopalliance.intercept.MethodInvocation接口,

    HelloAroundAdvice.java如下:

    package com.lei.demo.aop.schema;
    
    import java.lang.reflect.Method;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class HelloAroundAdvice implements MethodInterceptor {
    
        public Object invoke(MethodInvocation arg0) throws Throwable {
    
            System.out.println("++++++before advice");
            arg0.proceed();
            System.out.println("++++++after advice");
            
            return null;
        }
    
    }

    Xml配置:Spring-AOP-Schema.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:aop="http://www.springframework.org/schema/aop"  
            xsi:schemaLocation="  
               http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
               http://www.springframework.org/schema/aop  
               http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
               
        <!-- 目标类 -->
        <bean id="helloService" class="com.lei.demo.aop.schema.HelloService" />
        
        <bean id="helloArroundAdvice" class="com.lei.demo.aop.schema.HelloAroundAdvice" />
        
        <!-- 配置切面 -->
        <aop:config>
            <aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
            <aop:advisor advice-ref="helloArroundAdvice" pointcut-ref="helloPointcut"/>
            
        </aop:config>
    </beans>

    仍然运行App.java,结果如下:

    ++++++before advice

    -----Hello World!-----

    ++++++after advice

  • 相关阅读:
    elasticsearch 7.8.X 数据类型与使用规范
    Elasticsearch 7.8.x 设置账号密码的问题。基础级安全
    swoft2.x 扩展elasticsearch 组件
    一张图理清js原型链(通过内置对象的引用关系)
    在webkit中如何避免触发layout(重排)
    no-cache、max-age=0、must-revalidate区别
    HTTP缓存机制[译文]
    v8 源码获取与build
    CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装 之 (四)问题汇总
    CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装 之 (三)Nginx负载均衡配置
  • 原文地址:https://www.cnblogs.com/flying607/p/7833700.html
Copyright © 2011-2022 走看看