基於xml的AOP實現

applicationContext.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.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
       <!-- 目標類 -->
       <bean id="userServiceId" class="com.study.spring_aspectj.a_xml.UserServiceImpl"></bean>
       <!-- 切面類 -->
       <bean id="myAspectId" class="com.study.spring_aspectj.a_xml.MyAspect"></bean>

       <!-- 
        <aop:aspect>將切面類聲明“切面” 從而獲得通知(方法)
            ref  切面類引用
       <aop:pointcut> 聲明一個切入點表達式
       expression 切入點表達式
       id  名稱,用於其他通知引用
        -->
       <aop:config>
        <aop:aspect ref="myAspectId">
            <aop:pointcut expression="execution(* com.study.spring_aspectj.a_xml.UserServiceImpl.*(..))" id="myPointcut"/>
            <!-- 前置通知 
                <aop:before method="myBefore" pointcut="" pointcut-ref=""/>
                method : 通知,及方法名
                pointcut:切入點表達式,此表達式只能當前通知
                pointcut-ref:切入點引用,可以與其他通知共享切入點

                通知方法可以有參數
            -->
            <!-- 前置通知 -->
<!--                <aop:before method="myBefore" pointcut-ref="myPointcut"/> -->

            <!-- 後置通知 -->
<!--                <aop:after-returning method="myAfterReturning" returning="ret" pointcut-ref="myPointcut"/> -->

                <!-- 環繞通知 -->
<!--                <aop:around method="myArount" pointcut-ref="myPointcut"/>    -->
            <!-- 拋出異常通知 -->
<!--            <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/> -->

            <!-- 最終通知 -->
            <aop:after method="myAfter" pointcut-ref="myPointcut"/>

        </aop:aspect>
       </aop:config>


</beans>

Aspect 代碼:

package com.study.spring_aspectj.a_xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知"+joinPoint.getSignature().getName());
    }

    public void myAfterReturning(JoinPoint joinPoint,Object ret){
        System.out.println("後置通知"+joinPoint.getSignature().getName()+",-->"+ret);
    }

    public Object myArount(ProceedingJoinPoint joinPoint) throws Throwable {
        //手動執行目標方法
        System.out.println("前");

        Object object = joinPoint.proceed();

        System.out.println("後");
        return object;
    }


    public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("拋出異常通知:"+e.getMessage());
    }

    public void myAfter(){
        System.out.println("after");
    }


}

service實現類:

package com.study.spring_aspectj.a_xml;

public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("addUser...");
    }

    @Override
    public void updateUser() {
        //int i = 10/0;
        System.out.println("updateUser...");
    }

    @Override
    public void deleteUser() {
        System.out.println("deleteUser...");
    }

}

測試類:

package com.study.spring_aspectj.a_xml;

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

public class TestSpringxml {
    @Test
    public void demo1(){
        String xmlPath = "com/study/spring_aspectj/a_xml/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        //獲得的是目標類
        UserService userService = (UserService) applicationContext.getBean("userServiceId");
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();



    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章