需求分析

  针对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. 解析 ABP vNext 依赖注入实现【属性注入】的原理

    前言 这几天闲来没事看看ABP vNext的文档和源码,关于关于依赖注入(属性注入)这块儿产生了兴趣. 我们都知道.Volo.ABP 依赖注入容器使用了第三方组件Autofac实现的.有三种注入方式, ...

  2. PageOffice 6 最简集成代码(VUE+Springboot)

    本文描述了PageOffice产品在(VUE+Springboot)前后端分离的项目中如何集成调用.调用PageOffice打开文件的主要核心代码是:后端Springboot项目中第6步和前端VUE项 ...

  3. 关于sass(scss)、less、postcss、stylus的简介与区别

    为什么会出现css预处理器 ​ CSS不是一种编程语言,仅仅只能用来编写网站样式,在web初期时,网站的搭建还比较基础,所需要的样式往往也很简单.但是随着用户需求的增加以及网站技术的升级,css一成不 ...

  4. salesforce零基础学习(一百三十九)Admin篇之Begins/Contains/Starts With 是否区分大小写

    本篇参考: https://help.salesforce.com/s/articleView?id=sf.customize_functions_begins.htm&type=5 http ...

  5. vue2前端导出带背景色表格 xlsx xlsx-style

    vue2 +elmentui+xlsx10.0.0+xlsx-style 坑有点多. xlsx10.0.0以后的版本 用require导入或者使用什么导入什么,不要import * xlsx全部导入 ...

  6. JDK动态代理的深入理解

    引入代理模式 代理模式是框架中经常使用的一种模式,动态代理是AOP(面向切面编程)思想的一种重要的实现方式,在我们常用的框架中也经常遇见代理模式的身影,例如在Spring中事务管理就运用了动态代理,它 ...

  7. Android Media Framework - 开篇

    前言 Android Media是一块非常庞大的内容,上到APP的书写,中到播放器的实现.封装格式的了解,下到编解码组件的封装.VPU API的了解,每块内容的学习都需要我们下很大的功夫.此外,我们还 ...

  8. 编译安装mysql5.7.20

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \ ...

  9. uview 滑动切换

    ```html <template> <view class="content"> <!-- <u-row justify="spac ...

  10. 鸿蒙HarmonyOS实战-ArkTS语言基础类库(并发)

    一.并发 并发是指在一个时间段内,多个事件.任务或操作同时进行或者交替进行的方式.在计算机科学中,特指多个任务或程序同时执行的能力.并发可以提升系统的吞吐量.响应速度和资源利用率,并能更好地处理多用户 ...