zoukankan      html  css  js  c++  java
  • Spring的AOP快速实现通用日志打印

    需求分析

      针对VideoService接口实现日志打印

    三个核心包

    • spring-aop:AOP核心功能,例如代理工厂
    • aspectjweaver:简单理解,支持切入点表达式
    • aspectjrt:简单理解,支持aop相关注解

    定义Service接口和实现类

    VideoService.java

    package net.cybclass.sp.servicce;
    
    import net.cybclass.sp.domain.Video;
    
    public interface VideoService {
        int save(Video video);
        Video findById(int id);
    }

    VideoServiceImpl.java

    package net.cybclass.sp.servicce;
    
    import net.cybclass.sp.domain.Video;
    
    public class VideoServiceImpl implements VideoService{
        public int save(Video video) {
            System.out.println("保存Video");
            return 0;
        }
    
        public Video findById(int id) {
            System.out.println("根据id找视频");
            return new Video();
        }
    }

    定义横切关注点

       <bean id="timeHandler" class="net.cybclass.sp.aop.TimeHandler"></bean>
        <bean id="videoService" class="net.cybclass.sp.servicce.VideoServiceImpl"></bean>
        <!-- aop的配置 -->
        <aop:config>
            <!--切面-->
            <aop:aspect id="timeAspect" ref="timeHandler">
                <!--连接点-->
                <aop:pointcut id="allMethodLogPointCut" expression="execution(* net.cybclass.sp.servicce.VideoService.*(..))"/>
                <!--前置通知-->
                <aop:before method="printBefore" pointcut-ref="allMethodLogPointCut"></aop:before>
                <!--后置通知-->
                <aop:after method="printBefore" pointcut-ref="allMethodLogPointCut"></aop:after>
            </aop:aspect>
        </aop:config>

    完整版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="timeHandler" class="net.cybclass.sp.aop.TimeHandler"></bean>
        <bean id="videoService" class="net.cybclass.sp.servicce.VideoServiceImpl"></bean>
        <!-- aop的配置 -->
        <aop:config>
            <!--切面-->
            <aop:aspect id="timeAspect" ref="timeHandler">
                <!--连接点-->
                <aop:pointcut id="allMethodLogPointCut" expression="execution(* net.cybclass.sp.servicce.VideoService.*(..))"/>
                <!--前置通知-->
                <aop:before method="printBefore" pointcut-ref="allMethodLogPointCut"></aop:before>
                <!--后置通知-->
                <aop:after method="printBefore" pointcut-ref="allMethodLogPointCut"></aop:after>
            </aop:aspect>
        </aop:config>
    </beans>

    引入相关包

           <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.5</version>
            </dependency>

    完整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>net.cybcclass</groupId>
        <artifactId>cyb_spring</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>5.2.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.2.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.5</version>
            </dependency>
        </dependencies>
    </project>

    演示

  • 相关阅读:
    深入学习webpack(四)
    深入学习webpack(三)
    深入学习webpack(二)
    深入学习webpack(一)
    (转载)路径中 斜杠/和反斜杠 的区别
    jquery中beforeSend和complete的使用 --- 提高用户体验&&设置请求头
    好的用户界面-界面设计的一些技巧
    使用vue Devtools
    vue脚手架 && 实例
    表单(中)-EasyUI Combogrid 组合网格、EasyUI Numberbox 数字框、EasyUI Datebox 日期框、EasyUI Datetimebox 日期时间框、EasyUI Calendar 日历
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13306527.html
Copyright © 2011-2022 走看看