美文网首页
Spring_day03_实例2:基于注解的AOP通知

Spring_day03_实例2:基于注解的AOP通知

作者: 背对背拥抱 | 来源:发表于2019-11-19 18:02 被阅读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;
    
    @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;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    @Component("logger")
    @Aspect
    public class Logger {
    
        @Pointcut("execution(* com.zl.service.impl.*.*(..))")
        private void pt1(){
        }
    
        /**
         * 前置通知
         */
    //    @Before("pt1()")
        public void beforePrintLog(){
            System.out.println("Logger类中的beforePrintLog方法执行了....前置通知");
        }
        /**
         * 后置通知
         */
    //    @AfterReturning("pt1()")
        public void afterReturningPrintLog(){
            System.out.println("Logger类中的afterReturningPrintLog方法执行了....后置通知");
        }
        /**
         * 异常通知
         */
    //    @AfterThrowing("pt1()")
        public void afterThrowingPrintLog(){
            System.out.println("Logger类中的afterThrowingPrintLog方法执行了....异常通知");
        }
        /**
         * 最终通知
         */
    //    @After("pt1()")
        public void afterPrintLog(){
            System.out.println("Logger类中的afterPrintLog方法执行了....最终通知");
        }
    
        /**
         * 环绕通知
         * @return
         */
        @Around("pt1()")
        public Object aroundPrintLog(ProceedingJoinPoint pjp){
            Object[] args = pjp.getArgs();
            try {
                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

    <?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"
           xmlns:context="http://www.springframework.org/schema/context"
           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
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--设置要扫描的包-->
        <context:component-scan base-package="com.zl"></context:component-scan>
    
        <!--开启注解-->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>
    

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

    2)当我们不使用环绕通知时,spring自带通知的执行顺序为前置通知-->最终通知-->后置/异常通知,也就是说最终通知后置通知异常通知之前执行,是错误的,我们需要注意。

    五、测试类:

    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();
        }
    }
    

    六、使用纯注解:

    当我们完全不使用xml配置时,需要用到一个新注解@EnableAspectJAutoProxy

    @Configuration 
    @ComponentScan(basePackages="com.zl") 
    @EnableAspectJAutoProxy 
    public class SpringConfiguration { 
    }
    

    相关文章

      网友评论

          本文标题:Spring_day03_实例2:基于注解的AOP通知

          本文链接:https://www.haomeiwen.com/subject/mmjgictx.html