Spring_day03_实例1:基于XML配置的AOP通知

2019-11-19  本文已影响0人  背对背拥抱

项目组织结构如下:

一、pom依赖:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zl</groupId>
    <artifactId>spring_day03_aop_xml</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
    </dependencies>

</project>

二、Service层:

com.zl.service.AccountService接口:

package com.zl.service;

public interface AccountService {

    /**
     * 模拟保存账户
     */
    void saveAccount();

    /**
     * 模拟更新账户
     * @param i
     */
    void updateAccount(int i);

    /**
     * 删除账户
     * @return
     */
    int  deleteAccount();
}

com.zl.service.AccountServiceImpl实现类:

package com.zl.service.impl;

import com.zl.service.AccountService;

public class AccountServiceImpl implements AccountService {
    @Override
    public void saveAccount() {
        System.out.println("执行了保存");
//        int i = 1/0;
    }

    @Override
    public void updateAccount(int i) {
        System.out.println("执行了更新"+i);

    }

    @Override
    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

三、日志通知类:

com.zl.util.Logger日志通知类:

package com.zl.utils;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {

    /**
     * 前置通知
     */
    public void beforePrintLog(){
        System.out.println("Logger类中的beforePrintLog方法执行了....前置通知");
    }
    /**
     * 后置通知
     */
    public void afterReturningPrintLog(){
        System.out.println("Logger类中的afterReturningPrintLog方法执行了....后置通知");
    }
    /**
     * 异常通知
     */
    public void afterThrowingPrintLog(){
        System.out.println("Logger类中的afterThrowingPrintLog方法执行了....异常通知");
    }
    /**
     * 最终通知
     */
    public void afterPrintLog(){
        System.out.println("Logger类中的afterPrintLog方法执行了....最终通知");
    }

    /**
     * 环绕通知
     * @return
     */
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        try {
            Object[] args = pjp.getArgs();
            System.out.println("Logger类中的aroundPrintLog方法执行了....前置通知");
            Object obj = pjp.proceed(args);
            System.out.println("Logger类中的aroundPrintLog方法执行了....后置通知");
            return obj;
        } catch (Throwable throwable) {
            System.out.println("Logger类中的aroundPrintLog方法执行了....异常通知");
            throwable.printStackTrace();
        }finally {
            System.out.println("Logger类中的aroundPrintLog方法执行了....最终通知");
        }
        return null;
    }
}

四、AOP主配置文件:

resources.bean.xml

<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="accountService" class="com.zl.service.impl.AccountServiceImpl"> </bean>

    <bean id="logger" class="com.zl.utils.Logger"></bean>

    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.zl.service.impl.*.*(..))"/>
        
        <aop:aspect id="logAdvice" ref="logger">
            <!--配置前置通知-->
            <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
            <!--配置后置通知-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>
            <!--配置异常通知-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
            <!--配置最终通知-->
            <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>

            <!--配置环绕通知-->
<!--            <aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>-->
        </aop:aspect>
    </aop:config>

</beans>

需要注意的是:当我们使用环绕通知时,其他四个通知无法使用,否则反之。

五、测试类:

import com.zl.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = app.getBean("accountService", AccountService.class);
        accountService.saveAccount();
    }
}

上一篇下一篇

猜你喜欢

热点阅读