AOP的两种配置方式:XML配置和Aspectj注解方式。

一、项目的目录:

二、文件配置

我们采用的是JDK代理,所以首先将接口和实现类代码附上:

public interface UserManager {  

    public void addUser(String userName,String password);  

    public void delUser(int userId);  

    public String findUserById(int userId);  

    public void modifyUser(int userId,String userName,String password);
}
public class UserManagerImpl implements UserManager {  

    @Override
public void addUser(String userName, String password) {
System.out.println("----UserManagerImpl.add()----");
} @Override
public void delUser(int userId) {
System.out.println("----UserManagerImpl.delUser()----");
} @Override
public String findUserById(int userId) { System.out.println("----UserManagerImpl.findUserById()----"); if(userId <= 0){
throw new IllegalArgumentException("该用户不存在");
}
return "jiuqiyuliang";
} @Override
public void modifyUser(int userId, String userName, String password) {
System.out.println("----UserManagerImpl.modifyUser()----");
} }

切面类(安全性检测,日志管理)

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class AspectJAdvice { /**
* Pointcut
* 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数
* 该方法就是一个标识,不进行调用
*/
@Pointcut("execution(* find*(..))")
private void aspectjMethod(){}; /**
* Before
* 在核心业务执行前执行,不能阻止核心业务的调用。
* @param joinPoint
*/
@Before("aspectjMethod()")
public void doBefore(JoinPoint joinPoint) {
System.out.println("-----doBefore.invoke-----");
System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of doBefore()------");
} /**
* Around
* 手动控制调用核心业务逻辑,以及调用前和调用后的处理,
*
* 注意:当核心业务抛异常后,立即退出,转向AfterAdvice
* 执行完AfterAdvice,再转到ThrowingAdvice
* @param pjp
* @return
* @throws Throwable
*/
@Around(value = "aspectjMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("-----doAround.invoke-----");
System.out.println(" 此处可以做类似于Before的事情"); //调用核心逻辑
Object retVal = pjp.proceed();
System.out.println(" 此处可以做类似于After的事情");
System.out.println("-----End of doAround()------");
return retVal;
} /**
* After
* 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice
* @param joinPoint
*/
@After(value = "aspectjMethod()")
public void doAfter(JoinPoint joinPoint) {
System.out.println("-----doAfter.invoke-----");
System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of doAfter()------");
} /**
* AfterReturning
* 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice
* @param joinPoint
*/
@AfterReturning(value = "aspectjMethod()", returning = "retVal")
public void doReturn(JoinPoint joinPoint, String retVal) {
System.out.println("-----doReturn().invoke-----");
System.out.println("Return Value: " + retVal);
System.out.println(" 此处可以对返回值做进一步处理");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of doReturn()------");
} /**
* 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息
*
* 注意:执行顺序在Around Advice之后
* @param joinPoint
* @param ex
*/
@AfterThrowing(value = "aspectjMethod()", throwing = "ex")
public void doThrowing(JoinPoint joinPoint, Exception ex) {
System.out.println("-----doThrowing().invoke-----");
System.out.println(" 错误信息:"+ex.getMessage());
System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of doThrowing()------");
}
}

我们配置完切面类之后,还需要将Spring的IOC和AOP结合:

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- 启用Spring对基于@AspectJ aspects的配置支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="userManager" class="com.tgb.spring.UserManagerImpl"></bean> <bean id="aspectJAdvice" class="com.tgb.spring.AspectJAdvice"></bean> </beans>

所有都完成之后,最重要的一步就是编写客户端,进行测试,看是否和我们预想的结果一致。

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager) factory.getBean("userManager");
//可以查找张三
userManager.findUserById(1); System.out.println("=====我==是==分==割==线====="); try {
// 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获
userManager.findUserById(0);
} catch (IllegalArgumentException e) {
}
}
}

三、执行结果

正常运行,无异常抛出(一)

不正常运行,有异常抛出(二)

四、Advice五种类型的运行顺序

1)无异常抛出:    Around——》Before——》执行核心业务——》Around——》After——》AfterReturning

2)有异常抛出:    Around——》Before——》执行核心业务——》After——》AfterThrowing

摘自:https://blog.csdn.net/jiuqiyuliang/article/details/43967369

spring的AOP——采用注解完成AOP的更多相关文章

  1. Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

    Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...

  2. spring学习笔记二 注解及AOP

    本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...

  3. 【SSH进阶之路】Spring的AOP逐层深入——采用注解完成AOP(七)

    上篇博文[SSH进阶之路]Spring的AOP逐层深入——AOP的基本原理(六),我们介绍了AOP的基本原理,以及5种通知的类型, AOP的两种配置方式:XML配置和Aspectj注解方式. 这篇我们 ...

  4. Spring学习笔记5——注解方式AOP

    第一步:注解配置业务类 使用@Component("Pservice")注解ProductService 类 package com.spring.service; import ...

  5. Spring学习--用 ASpectJ 注解实现 AOP

    用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...

  6. spring框架学习(四)——注解方式AOP

    注解配置业务类 使用@Component("s") 注解ProductService 类 package com.how2java.service; import org.spri ...

  7. Spring AOP 针对注解的AOP

    我也忘记是从哪里扒来的代码,不过有了这个思路,以后可以自己针对 Controller 还有 Service层的任意 方法进行代理了 package pw.jonwinters.aop; import ...

  8. spring boot通过自定义注解和AOP拦截指定的请求

    一 准备工作 1.1 添加依赖 通过spring boot创建好工程后,添加如下依赖,不然工程中无法使用切面的注解,就无法对制定的方法进行拦截 <dependency> <group ...

  9. Spring的AOP机制---- AOP的注解配置---- AOP的注解配置

    3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...

随机推荐

  1. 为SourceInsight添加多行注释功能菜单

    由于项目看代码主要使用的是Source Insight,习惯了其他编辑器的多行注释功能,对此感到很不习惯,查询网上程序,可以自行添加. 1.打开project,选择base项目中的utils.em,添 ...

  2. mock.js 模拟数据

    1. 劫持请求,返回模拟数据: 用于前后台对接前数据模拟 相比于静态json文件而言:代码完成后不必修改源文件对应的接口调用.可模拟增删改查 2.实例代码 <!doctype html> ...

  3. mysql5.5.x.zip 解压版安装教程

    一,前言 记一次安装解压版的mysql 5.5的经过,参考了一些文章,也遇到了一些错误,最终都安装成功了.在这里记录一下安装的过程,一方面自己做一个记录,领一方面给大家提供一份参考. 二,环境 1,w ...

  4. 利用selenium 爬取豆瓣 武林外传数据并且完成 数据可视化 情绪分析

    全文的步骤可以大概分为几步: 一:数据获取,利用selenium+多进程(linux上selenium 多进程可能会有问题)+kafka写数据(linux首选必选耦合)windows直接采用的是写my ...

  5. 批处理 使用默认浏览器 打开html文件

    @echo offfor /f "tokens=3,4" %%a in ('"reg query HKEY_CLASSES_ROOT\http\shell\open\co ...

  6. zencart移站后批量替换数据库中网址、电子邮箱、重置用户密码

    -- SEO标签中网址替换 update categories_description set categories_description=replace(categories_descriptio ...

  7. Codeforces Round #593 (Div. 2) D. Alice and the Doll

    题目:http://codeforces.com/problemset/problem/1236/D思路:机器人只能按照→↓←↑这个规律移动,所以在当前方向能够前进的最远处即为界限,到达最远处右转,并 ...

  8. 高性能mysql 第1章 mysql架构与历史

    mysql逻辑架构图: 第一层 客户端 第二层(服务层):针对所有类型的存储引擎可以公共提取的部分.将存储引擎抽离之后的其他部分都在这里.如:查询解析,分析优化,内置函数,存储过程,触发器,视图. 第 ...

  9. head&tail命令

    1.tail tail命令用途是按照要求将指定的文件的最后部分输出到标准设备, 一般是终端,通俗来讲,就是把某个档案文件的最后几行显示到终端上, 如果该档案有更新,tail会自动刷新,确保你看到最新的 ...

  10. 【LuoguP5328】[ZJOI2019]浙江省选

    题目链接 题意 给你一堆斜率和纵截距都为正的直线 ,求对于一个条直线是否存在一个 x 使得在这条直线在 x 处能是前 m 大,输出最高能够达到的排名(排名定义为在 x 处严格大于自己的直线条数+1) ...