Spring AOP實現方式四之注入式AspectJ切面【附源碼】

如今咱們要講的是第四種AOP實現之注入式AspectJ切面

經過簡單的配置就能夠實現AOP了。

源碼結構:

image

一、首先咱們新建一個接口,love 談戀愛接口。

package com.spring.aop;

/**
 * 談戀愛接口
 * 
 * @author Administrator
 *
 */
public interface Love
{

    /*
     * 談戀愛方法
     */
    void fallInLove();

}

二、咱們寫一個Person類實現Love接口

package com.spring.aop;

/**
 * 人對象
 * @author luwenbin006@163.com
 *
 */
public class Person implements Love
{

    /*
     * 重寫談戀愛方法
     * @see com.spring.aop.Love#fallInLove()
     */
    public void fallInLove()
    {
        System.out.println("談戀愛了...");
    }

}

三、下面咱們來寫aop 註解通知類  分別是配置中的 執行前

執行後 執行先後 執行拋出異常

package com.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 註解方式 aop通知類
 * @author luwenbin006@163.com
 *
 */
public class LoveHelper
{

    //在調用方法以前執行 執行攔截包com.spring.aop.*下全部的方法
    public void doBefore(JoinPoint point) throws Throwable
    {
        System.out.println("談戀愛以前必需要彼此瞭解!");
    }
    
    //在調用方法先後執行
    public Object doAround(ProceedingJoinPoint point) throws Throwable
    {
        if(point.getArgs().length>0)
        {
            
            return point.proceed(point.getArgs());
            
        }else
        {
            
            return point.proceed();
        }
    }
    
    //在調用方法以後執行
    public void doAfter(JoinPoint point) throws Throwable
    {
        System.out.println("咱們已經談了5年了,最終仍是分手了!");
        //System.out.println("咱們已經談了5年了,最終步入告終婚的殿堂!");
    }
    
    //當拋出異常時被調用
    public void doThrowing(JoinPoint point, Throwable ex)
    {
        System.out.println("doThrowing::method "
                + point.getTarget().getClass().getName() + "."
                + point.getSignature().getName() + " throw exception");
        System.out.println(ex.getMessage());
    }

}

四、配置好application.xml  配置好AOP注入模式和相應的bean aop通知類。

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <!-- 配置bean -->
    <bean id="person" class="com.spring.aop.Person">
    </bean>
    <!-- 配置通知方法類 -->
    <bean id="loveHelper" class="com.spring.aop.LoveHelper">
    </bean>

    <aop:config>
        <aop:aspect id="configAspect" ref="loveHelper">
            <!--配置com.spring.aop包下全部類或接口的全部方法 -->
            <aop:pointcut id="loveServices" expression="execution(* com.spring.aop.*..*(..))" />
            <aop:before pointcut-ref="loveServices" method="doBefore" />
            <aop:after pointcut-ref="loveServices" method="doAfter" />
            <aop:around pointcut-ref="loveServices" method="doAround" />
            <aop:after-throwing pointcut-ref="loveServices"
                method="doThrowing" throwing="ex" />
        </aop:aspect>
    </aop:config>

</beans>

五、經過測試類測試下咱們的代碼~~~

package com.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.aop.Love;

/**
 * aop測試方法類
 * @author luwenbin006@163.com
 *
 */
public class LoveTest
{

    public static void main(String[] args)
    {
        //加載spring配置文件
        ApplicationContext appCtx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        //獲得person對象
        Love love = (Love) appCtx.getBean("person");
        //調用談戀愛方法
        love.fallInLove();
    }
}

六、看控制檯輸出結果 調用了咱們定義的aop攔截方法~~~ ok了

image

 

七、源碼下載地址:源碼下載css

相關文章
相關標籤/搜索