需求分析

  针对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>

演示

Spring的AOP快速实现通用日志打印的更多相关文章

  1. 【spring】aop切面通知,日志处理

    1.spring的切面编程 概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115 2.所需要的jar包 maven引入jar ...

  2. spring boot:使用log4j2做异步日志打印(spring boot 2.3.1)

    一,为什么要使用log4j2?     log4j2是log4j的升级版,     升级后更有优势:     性能更强/吞吐量大/支持异步     功能扩展/支持插件/支持自定义级别等     这些优 ...

  3. spring boot aop 自定义注解 实现 日志检验 权限过滤

    核心代码: package com.tran.demo.aspect; import java.lang.reflect.Method; import java.time.LocalDateTime; ...

  4. Spring系列.AOP使用

    AOP简介 利用面向对象的方法可以很好的组织代码,也可以继承的方式实现代码重用.但是项目中总是会出现一些重复的代码,并且不太方便使用继承的方式把他们重用管理起来,比如说通用日志打印,事务处理和安全检查 ...

  5. 采用Spring AOP+Log4j记录项目日志

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...

  6. Spring AOP+Log4j记录项目日志

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...

  7. spring AOP知识点总结以及日志的输出

    AOP的作用就是在基于OCP在不改变原有系统核心业务代码的基础上动态添加一些扩展功能.通常应用于日志的处理,事务处理,权限处理,缓存处理等等 首先,使用AOP需要添加的依赖有:spring-conte ...

  8. 基于XML配置的AOP实现日志打印

    Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...

  9. 简单的aop实现日志打印(切入点表达式)

    Spring中可以使用注解或XML文件配置的方式实现AOP. 1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.or ...

  10. AOP与Filter拦截请求打印日志实用例子

    相信各位同道在写代码的时候,肯定会写一些日志打印,因为这对往后的运维而言,至关重要的. 那么我们请求一个restfull接口的时候,哪些信息是应该被日志记录的呢? 以下做了一个基本的简单例子,这里只是 ...

随机推荐

  1. uniapp微信小程序uni.request捕获500异常

    通常使用ajax,axios等进行服务请求,500错误或者其他的错误都会直接进入到错误通道里头,比如ajax异常的话会进入到error的回调函数里头,axios异常会进行到catch里头,一开始以为u ...

  2. 高效运维_AIRIOT智慧电力运维解决方案

    可再生能源的引入带来了能源生产的去中心化和分散化趋势,同时也带来了能源输出的波动性和不确定性.电力运维因此需要更加灵活.智能的解决方案,以适应可再生能源的集成,确保电力系统的稳定运行,传统的电力运维管 ...

  3. 鸿蒙HarmonyOS实战-Stage模型(开发卡片事件)

    一.开发卡片事件 HarmonyOS元服务卡片页面(Metaservice Card Page)是指在HarmonyOS系统中,用于展示元服务的页面界面.元服务是指一组提供特定功能或服务的组件,例如天 ...

  4. .net程序员学习java篇一(搭建SSM)

    一.安装IDE 相比于.net环境的一气呵成,java可能麻烦一点,这里记录下来,避免萌新踩坑 1.1安装JDK,这里不要玩什么花哨,老老实实选个大众版(Oracle JDK1.8x),设置环境变量, ...

  5. 本地项目文件上传到git

    初始化项目: git init 与服务器项目关联:git remote add origin "http://**************************/r/ruoyi.git&q ...

  6. jquery加载页面时触发事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. AI绘图之Midjourney初体验

    Midjourney (MJ) 使用笔记 最近尝试了 Midjourney 绘图,简单记录下使用流程. 注册及登陆 首先是账号注册和登陆,基本上就是一路下一步,唯一需要注意的是加入MJ频道,具体流程为 ...

  8. Qt-qrencode开发-生成、显示二维码📀

    Qt-qrencode开发-生成二维码 目录 Qt-qrencode开发-生成二维码 1.概述 2.实现效果 3.编译qrencode 4.在QT中引入编译为静态库的QRencode 5.在Qt中直接 ...

  9. docker——存储配置与管理

    docker存储配置与管理 查看docker info [root@hmm overlay2]# docker info Client: Docker Engine - Community Versi ...

  10. 简单实现Viper配置管理

    本文由 ChatMoney团队出品 简介 前面实现的一个简易suno-api.是使用cookie来获取suno-token发起请求的.当时并没有通过配置的方式来获取cookie,而是直接在代码中写死了 ...