spring aop的兩種寫法aspect和advisor

劍握在手發表於2017-11-14

本文轉自: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

 

相關文章