1. 程式人生 > >AOP之AspectJ(XML)

AOP之AspectJ(XML)

package com.imooc.aspectJ.demo2;

public interface CustomerDao {

    public void save();
    public String update();
    public void delete();
    public void findOne();
    public void findAll();

}
package com.imooc.aspectJ.demo2;

public class CustomerDaoImpl implements CustomerDao {
    
    public void save() {
        System.out.println("儲存客戶...");
    }

    public String update() {
        System.out.println("修改客戶...");
        return "spring";
    }

    public void delete() {
        System.out.println("刪除客戶...");
    }

    public void findOne() {
        System.out.println("查詢一個客戶...");
//       int a = 1/ 0;
    }

    public void findAll() {
        System.out.println("查詢多個客戶...");
//        int b = 1/0;
    }
    
}
package com.imooc.aspectJ.demo2;

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

public class MyAspectXml {

    // 前置通知
    public void before(JoinPoint joinPoint){
        System.out.println("XML方式的前置通知=============="+joinPoint);
    }

    // 後置通知
    public void afterReturing(Object result){
        System.out.println("XML方式的後置通知=============="+result);
    }

    // 環繞通知
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("XML方式的環繞前通知==============");
        Object obj = joinPoint.proceed();
        System.out.println("XML方式的環繞後通知==============");
        return obj;
    }

    // 異常丟擲通知
    public void afterThrowing(Throwable e){
        System.out.println("XML方式的異常丟擲通知============="+e.getMessage());
    }

    // 最終通知
    public void after(){
        System.out.println("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">

    <!--XML的配置方式完成AOP的開發-->
    <!--配置目標類-->
    <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>

    <!--配置切面類-->
    <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>

    <!--aop的相關配置-->
    <aop:config>
        <!--配置切入點-->
        <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
        <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
        <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
        <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
        <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>
        <!--配置AOP的切面-->
        <aop:aspect ref="myAspectXml">
            <!--配置前置通知-->
            <aop:before method="before" pointcut-ref="pointcut1"/>
            <!--配置後置通知-->
            <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
            <!--配置環繞通知-->
            <aop:around method="around" pointcut-ref="pointcut3"/>
            <!--配置異常丟擲通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
            <!--配置最終通知-->
            <aop:after method="after" pointcut-ref="pointcut5"/>
        </aop:aspect>

    </aop:config>

</beans>
package com.imooc.aspectJ.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext2.xml")
public class SpringDemo2 {

    @Resource(name="customerDao")
    private CustomerDao customerDao;

    @Test
    public void demo1(){
        customerDao.save();
        customerDao.update();
        customerDao.delete();
        customerDao.findOne();
        customerDao.findAll();
    }

}
執行結果:
XML方式的前置通知==============execution(void com.imooc.aspectJ.demo2.CustomerDao.save())
儲存客戶...
修改客戶...
XML方式的後置通知==============spring
XML方式的環繞前通知==============
刪除客戶...
XML方式的環繞後通知==============
查詢一個客戶...
查詢多個客戶...
XML方式的最終通知=================