spring(aop面向切面编程)
aop很早有研究过,但是最近想回顾下,顺便记录下,aop的优点有很多,实用性也很广,就好比最早在公司没有使用aop的时候没个业务层都要写try catch来捕获异常,来处理异常,甚至于记录异常或者日志,那是一件相当繁琐的事情,但是当我们使用aop,讲异常日志处理机制通过切面技术来脱离出业务层,让业务层只来管控自己的业务逻辑,这样即减轻了开发上的效率问题,又使得代码上更加清晰。当然aop用处还不仅仅只有这些,比如权限控制、事务控制,这些将在后续有时间的时候继续记录下来。
下面就来介绍aop的基本配置和使用,aop的配置也是相当简单,基本一步就能搞定,项目就借用之前ssm+redis的基础了,有部分文件也就不一一写了,可以查阅ssm+redis(cache方式)那一章节。
1、spring-source.xml
<!-- aop切面类 -->
<bean id="loggerAop" class="com.tp.soft.aop.LoggerAop"></bean> <aop:config>
<aop:aspect id="loggerAop" ref="loggerAop">
<!-- 正则表达式匹配 -->
<aop:pointcut expression="execution(* com.tp.soft.service..*.*(..))" id="mypoint"/>
<aop:before method="doBefore" pointcut-ref="mypoint"/>
<aop:around method="doAround" pointcut-ref="mypoint"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="mypoint"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="mypoint" throwing="e"/>
</aop:aspect>
</aop:config>
2、LoggerAop.java
package com.tp.soft.aop; import java.io.IOException;
import java.sql.SQLException; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException; import com.tp.soft.common.exception.BaseServiceException; public class LoggerAop { private Logger LOG = LoggerFactory.getLogger(Logger.class); //前置通知
public void doBefore(JoinPoint jp){
System.out.println(jp.getTarget().getClass().getName());
} public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕开始");
Object proceed = pjp.proceed();
System.out.println("环绕结束");
return proceed;
} public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
} public void doAfterThrowing(JoinPoint jp, Throwable e){
String errStr = "";
String err_detail = "";
if (e.getClass().equals(DataAccessException.class)) {
errStr = "数据库操作失败!";
} else if (e.getClass().toString()
.equals(NullPointerException.class.toString())) {
errStr = "调用了未经初始化的对象或者是不存在的对象!";
} else if (e.getClass().equals(IOException.class)) {
errStr = "IO异常!";
} else if (e.getClass().equals(ClassNotFoundException.class)) {
errStr = "指定的类不存在!";
} else if (e.getClass().equals(ArithmeticException.class)) {
errStr = "数学运算异常!";
err_detail = errStr;
} else if (e.getClass().equals(ArrayIndexOutOfBoundsException.class)) {
errStr = "数组下标越界!";
err_detail = errStr;
} else if (e.getClass().equals(IllegalArgumentException.class)) {
errStr = "方法的参数错误!";
err_detail = errStr;
} else if (e.getClass().equals(ClassCastException.class)) {
errStr = "类型强制转换错误!";
err_detail = errStr;
} else if (e.getClass().equals(SecurityException.class)) {
errStr = "违背安全原则异常!";
err_detail = errStr;
} else if (e.getClass().equals(SQLException.class)) {
errStr = "操作数据库异常!";
err_detail = errStr;
} else if (e.getClass().equals(NoSuchMethodError.class)) {
errStr = "方法末找到异常!";
err_detail = errStr;
} else if (e.getClass().equals(InternalError.class)) {
errStr = "Java虚拟机发生了内部错误";
err_detail = errStr;
} else if (e.getClass().equals(BaseServiceException.class)){
errStr = e.getMessage();
err_detail = e.getMessage();
} else {
errStr = e.getCause().getMessage();
err_detail = e.getCause().getLocalizedMessage();
} System.err.println("aop打印错误:"+errStr);
System.err.println("aop打印错误详细:"+err_detail); //可以操作数据库记录
}
}
测试打印

异常测试:
修改UserMapper.xml 的sql语句 结尾加上一个;


spring(aop面向切面编程)的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
- 详细解读 Spring AOP 面向切面编程(一)
又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
随机推荐
- MySQL练习题1
以下SQL操作均在MYSQL上测试过 首先是表定义 1.创建student和score表 CREATE TABLE student ( id ) NOT NULL UNIQUE PRIMARY KEY ...
- thinkphp 百度地图Api坐标计算 A坐标距离B坐标多少公里 并按照距离近的排序 坐标排序 外部字段排序
感谢我磊哥 函数封装方法: //计算距离 /* **$a 可多数坐标 就是可数组类型的 ***$b 是登录者的坐标 ***ps: lat经度 lng纬度 经度在前纬度在后 *** ***/ funct ...
- 基于bootstrap-treeview做的一个漂亮的无限分类树层级联动菜单
2017年12月11日09:59:15 因为工作需要把原来的bootstrap-treeview做了一些小改动,方便后台开发人员使用 最终效果,看起来还行,但是其实不是特别友好对用户来说,但是对开发者 ...
- Spring Boot 自动配置原理(精髓)
一.自动配置原理(掌握) SpringBoot启动项目会加载主配置类@SpringBootApplication,开启@EnableAutoConfiguration自动配置功能 @EnableAut ...
- stellar.js 视差滚动
1.引入包 <script src="js/jquery.min.js"></script> <script src="js/jquery. ...
- 使用Vivado初探ZedBoard的OLED驱动
一.原理简介 Vivado版本:2016.2 OLED型号:128*32的UG-2832HSWEG04 ZedBoard的OLED部分电路原理图如下:(需要我们关心的是我用红色椭圆标注出来的3处,一共 ...
- spring--给配置文件.properties加密
11111111111编写类并继承PropertyPlaceholderConfigurer.java package com.xx.encryptDecrypt; import java.util. ...
- webstorm 配置 开发微信小程序
默认情况下,webstorm是不支持wxml和wxss的文件类型,不会有语法高亮 设置高亮 除了高亮,还需要代码提示, 所幸已经有前辈整理了小程序的代码片段,只需要导入其安装包即可使用,包文件路径如下 ...
- python中Hadamard product和matrix product的区分
先简单说一下Hadamard product: (参照维基百科:https://en.wikipedia.org/wiki/Hadamard_product_(matrices)) 而matrix ...
- 十一、无事勿扰,有事通知(2)——KVO
概述 Key-Value-Observe,简称KVO,和上节介绍的Notification师出同门,主要目的都是为了实现观察者模式. 虽说是同门师兄弟,但是各自精通的技艺却是各不相同的. 不像Noti ...