Spring切面编程实践【原创】
定义
什么叫Spring面向切面编程(AOP),请自行百度,这边就不做详细介绍了。
场景
有两个对象,字典和工程信息Bean,每次新增或修改对象时,记录新增和修改的时间。
基类定义
package myTest.entity; import lombok.Data; import java.time.LocalTime; /**
* entity基类
*/
@Data
public class BaseEntity {
private Long id;
private LocalTime createTime;
private LocalTime updateTime;
}
子类定义
package myTest.entity; import lombok.Data; /**
* 字典信息
*/
@Data
public class DictEntity extends BaseEntity{
private String key;
private String value;
}
package myTest.entity; import lombok.Data; /**
* 项目信息
*/
@Data
public class ProjectEntity extends BaseEntity {
private String projectName;
}
注:这边使用了lombok,不熟悉的同学可以查看https://www.cnblogs.com/meituan/p/10510889.html
常规做法
service基类
package myTest.service;
public interface BaseService<T> {
void save(T entity);
void update(T entity);
}
字典Service实现
package myTest.service;
import myTest.entity.DictEntity;import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalTime;
public class DictService implements BaseService<DictEntity> {
@Autowired
private DictDao dao;
@Override
public void save(DictEntity entity){
entity.setCreateTime(LocalTime.now());
entity.setUpdateTime(LocalTime.now());
dao.save(entity);
}
@Override
public void update(DictEntity entity){
entity.setUpdateTime(LocalTime.now());
dao.update(entity);
}
}
工程Service实现
package myTest.service; import myTest.entity.ProjectEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.time.LocalTime; @Service
public class ProjectService implements BaseService<ProjectEntity> { @Autowired
private ProjectDao dao; @Override
public void save(ProjectEntity entity){
entity.setCreateTime(LocalTime.now());
entity.setUpdateTime(LocalTime.now());
dao.save(entity);
} @Override
public void update(ProjectEntity entity){
entity.setUpdateTime(LocalTime.now());
dao.update(entity);
} }
反思
常规做法做了重复劳动工作,如果entity子类更多的话,重复劳动会更多,而且很不容易维护,这时候就可以用到Spring的切面编程思想。
使用Spring AOP解决问题
大致思路可以这样,当Service执行save和update方法的时候,定义一个切点,在执行到这个切点的时候,统一加入更新时间和新增时间的控制,一处增加,多处使用,省时省力,而且便于维护。上代码!
- 首先,在pom文件中引入aspect依赖的jar包,如下
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
- 其次,定义切点和前置通知
package myTest; import myTest.entity.BaseEntity;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import java.time.LocalTime;
import java.util.Arrays;
import java.util.List; /**
* 定义切点和前置通知,统一修改新增时间和修改时间
*/
@Aspect
@Component
public class Asepect { @Pointcut("execution(* myTest.service.DictService.save*(..)) || execution(* myTest.service.DictService.update*(..)) || " +
"execution(* myTest.service.ProjectService.save*(..)) || execution(* myTest.service.ProjectService.update*(..))")
public void addAndUpdatePointCut(){} @Before("addAndUpdatePointCut()")
public void before(JoinPoint joinPoint) {
//获取方法名
String mathName = joinPoint.getSignature().getName();
//获取参数列表
List<Object> args = Arrays.asList(joinPoint.getArgs());
for (Object o :
args) {
if (o instanceof BaseEntity) {
((BaseEntity) o).setUpdateTime(LocalTime.now());
if (mathName.startsWith("save")) {
((BaseEntity) o).setCreateTime(LocalTime.now());
}
}
}
} }
就这样两个简单的步骤,就可以解决原始方法带来的重复工作和不易维护的问题,是不是很爽?
【纯手打,请多多支持,谢谢】
Spring切面编程实践【原创】的更多相关文章
- Method Swizzling和AOP(面向切面编程)实践
Method Swizzling和AOP(面向切面编程)实践 参考: http://www.cocoachina.com/ios/20150120/10959.html 上一篇介绍了 Objectiv ...
- spring切面编程AOP 范例一
参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...
- Spring切面编程步骤
什么是面向切面编程 面向对象的编程主要注重核心业务,而面向切面编程主要关注一些不是核心的业务,但又是必须的辅助功能,比如一个完整的系统中,记录平时系统运行时抛出的异常,需要我们去记录,以便我们对系统尽 ...
- spring aop使用,spring aop注解,Spring切面编程
================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...
- spring切面编程
xml配置实现 先写三个类 public String amethod(String s) { System.out.println("This is AAAAAAAAAAAAAAAA&q ...
- Spring切面编程AOP
- Spring切面编程Aspect之@Before和@Around用法
查看dao层使用的sql import java.util.Arrays; import org.apache.commons.lang.ArrayUtils; import org.aspectj. ...
- spring aop 切面编程中获取具体方法的方法
spring 切面编程中获取具体方法的方法 工作中,使用环绕通知,用来捕获异常,然后通过获取方法的返回值,返回不同的数据给到调用方. 由于方法的返回值不同,我们处理异常时,也需要返回不同的格式. 这时 ...
- spring框架应用系列三:切面编程(带参数)
本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7786715.html 解决问题 1.分离业务监控与业务处理.简单点 ...
随机推荐
- day05-数据类型与操作
- goroutine 知识点
goroutine: 协程是用户态的轻量级线程: 协程之间,通过消息传递进行通信(大多数语言通过共享内存进行通信) select 使用的场景: 正常处理业务的 routine 退出(超时.不满足某条件 ...
- repo/repo init-解决同步源码Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle
以下转自:http://www.cnblogs.com/dinphy/p/5669384.html 问题: fatal: Cannot get https://gerrit.googlesource. ...
- 【java】package
总结: 包与包之间进行访问,被访问的包中的类以及类中的成员,需要public修饰. 不同包中的子类还可以直接访问父类中被protected权限修饰的成员. 包与包之间可以使用的权限只有两种,publi ...
- Azure CosmosDB (5) 高可用性
<Windows Azure Platform 系列文章目录> Azure Cosmos DB 透明地复制与您的Cosmos帐户关联的所有Azure区域中的数据. Cosmos DB 对数 ...
- python基础知识5---数据类型、字符编码、文件处理
阅读目录 一 引子 二 数字 三 字符串 四 列表 五 元组 六 字典 七 集合 八 数据类型总结 九 运算符 十 字符编码 十一 文件处理 十二 作业 一 引子 1 什么是数据? x=10,10 ...
- LAB1 partIII
partIII 实现 分发MapReduce 任务,实现 schedule() 方法在 mapreduce/schedule.go. schedule()函数的职责是把任务分给可用的worker. s ...
- 基于fastadmin快速搭建后台管理
FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架:开发文档 下面对环境搭建简要概述,希望后来者能少走弯路: 1. 百度搜索最新版wampserver, 安装并启动 ...
- CDNI - RFC7336翻译
CDNI框架 摘要 本文档提出了CDNI的一个框架.框架的目的是提供对CDNI问题空间的总体描述,和描述CDN互连所需的各种组件之间的 关系.CDNI需要指定接口和机制解决诸如请求路由,分发交换元数据 ...
- Shiro与基本web环境整合登陆验证实例
1. 用maven导入Shiro依赖包 <dependency> <groupId>org.apache.shiro</groupId> <artifactI ...