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>
演示

Spring的AOP快速实现通用日志打印的更多相关文章
- 【spring】aop切面通知,日志处理
1.spring的切面编程 概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115 2.所需要的jar包 maven引入jar ...
- spring boot:使用log4j2做异步日志打印(spring boot 2.3.1)
一,为什么要使用log4j2? log4j2是log4j的升级版, 升级后更有优势: 性能更强/吞吐量大/支持异步 功能扩展/支持插件/支持自定义级别等 这些优 ...
- spring boot aop 自定义注解 实现 日志检验 权限过滤
核心代码: package com.tran.demo.aspect; import java.lang.reflect.Method; import java.time.LocalDateTime; ...
- Spring系列.AOP使用
AOP简介 利用面向对象的方法可以很好的组织代码,也可以继承的方式实现代码重用.但是项目中总是会出现一些重复的代码,并且不太方便使用继承的方式把他们重用管理起来,比如说通用日志打印,事务处理和安全检查 ...
- 采用Spring AOP+Log4j记录项目日志
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...
- Spring AOP+Log4j记录项目日志
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...
- spring AOP知识点总结以及日志的输出
AOP的作用就是在基于OCP在不改变原有系统核心业务代码的基础上动态添加一些扩展功能.通常应用于日志的处理,事务处理,权限处理,缓存处理等等 首先,使用AOP需要添加的依赖有:spring-conte ...
- 基于XML配置的AOP实现日志打印
Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...
- 简单的aop实现日志打印(切入点表达式)
Spring中可以使用注解或XML文件配置的方式实现AOP. 1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.or ...
- AOP与Filter拦截请求打印日志实用例子
相信各位同道在写代码的时候,肯定会写一些日志打印,因为这对往后的运维而言,至关重要的. 那么我们请求一个restfull接口的时候,哪些信息是应该被日志记录的呢? 以下做了一个基本的简单例子,这里只是 ...
随机推荐
- MATLAB txt文件抽稀并分为多个txt文件
chouxi.m a = load("file.txt"); % len = length(a); interval = 9;%间隔+1 b = a(1:interval:end, ...
- 通过 Wireshark 解密 Kerberos 票据
前言 在使用 Wireshark 分析 Active Directory 的 Kerberos 的流量时,会遇到加密票据的情况,这对进一步探究 AD 下的漏洞篡改事件的详细过程造成了影响.在查询资料时 ...
- 【c++】求解八皇后问题
为:在8×8格的国际象棋上摆放8个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法.一共92个解 解决思路:一层层回溯,采用深度优先的递归算法. 动态分配的数 ...
- 如何更加优雅的使用 SSH 进行登录
引言 我们在日常的开发过程中,很多时候需要连接服务器查看日志或者在服务器上调试代码.但是,使用 ssh 命令登录服务器每次都需要输出密码,就比较繁琐.因此我们可以使用 sshpass 通过参数指定密码 ...
- Python爬图片(面向对象版)
import requests from lxml import etree from threading import Thread class Spider(object): def __init ...
- Bi-encoder vs Cross encoder?
本文永久地址:https://wanger-sjtu.github.io/encoder-cross-bi/ Bi-encoder和Cross-encoder是在自然语言理解任务模型的两种不同方法,在 ...
- kettle从入门到精通 第十四课 kettle kafka 生产者和消费者
1.本节课讲解kafka生产者和消费者两个步骤.这两个组件可以实现数据实时同步(后续课程会讲解). 2.kafka producer 步骤 1)step name:自定义名称 2)connection ...
- element el-input 去掉边框
element样式还是蛮好的,只是有时候我们需要做一些调整,比如,el-input 的边框,官网是这样子的 我们需要去掉这个边框 试了常用的:border: none: 以及:outline:non ...
- unsupported operand type(s) for +: 'function' and 'str'
unsupported operand type(s) for +: 'function' and 'str' 报错解释:这个错误表明你尝试将一个函数和一个字符串进行加法操作,在Python中,加法不 ...
- Java映射 转换post response T data
Java映射 转换post response data 接上篇Java泛型对象在http请求和响应对象中的封装https://www.cnblogs.com/oktokeep/p/17688322.h ...