spring的AOP——采用注解完成AOP
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的更多相关文章
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- spring学习笔记二 注解及AOP
本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...
- 【SSH进阶之路】Spring的AOP逐层深入——采用注解完成AOP(七)
上篇博文[SSH进阶之路]Spring的AOP逐层深入——AOP的基本原理(六),我们介绍了AOP的基本原理,以及5种通知的类型, AOP的两种配置方式:XML配置和Aspectj注解方式. 这篇我们 ...
- Spring学习笔记5——注解方式AOP
第一步:注解配置业务类 使用@Component("Pservice")注解ProductService 类 package com.spring.service; import ...
- Spring学习--用 ASpectJ 注解实现 AOP
用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...
- spring框架学习(四)——注解方式AOP
注解配置业务类 使用@Component("s") 注解ProductService 类 package com.how2java.service; import org.spri ...
- Spring AOP 针对注解的AOP
我也忘记是从哪里扒来的代码,不过有了这个思路,以后可以自己针对 Controller 还有 Service层的任意 方法进行代理了 package pw.jonwinters.aop; import ...
- spring boot通过自定义注解和AOP拦截指定的请求
一 准备工作 1.1 添加依赖 通过spring boot创建好工程后,添加如下依赖,不然工程中无法使用切面的注解,就无法对制定的方法进行拦截 <dependency> <group ...
- Spring的AOP机制---- AOP的注解配置---- AOP的注解配置
3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...
随机推荐
- shiro配置学习
一.shiro的配置 1.shiro的web过滤 实例化ShiroFilterFactoryBean 设置securityManager.loginUrl.unauthorizedUrl.sucess ...
- Nginx安装目录详解
Nginx安装目录详解 1. 查看有关nginx的所有目录列表,输入命令 rpm -ql nginx 可以查看有关nginx目录信息,但是注意 这种命令只能是在基于yum安装的方式才可以. 2. 下 ...
- 基于Zabbix 3.2.6版本的Discovery
作用:用于发现某IP网段内存活并且满足一定条件的主机,发现后进行加入到zabbix server进行监控. 操作步骤: 创建[自动发现规则] 为新建的自动发现规则创建[Action] 操作步骤图文 ...
- python之multiprocessing多进程
multiprocessing 充分利用cpu多核一般情况下cpu密集使用进程池,IO密集使用线程池.python下想要充分利用多核CPU,就用多进程. Process 类Process 类用来描述一 ...
- easyui datagrid连续删除问题
如果在datagrid中直接将index传给easyui自带的deletRow方法来删除当前点击行,一开始并没有问题,但是当连续删除的时候就或出问题了. 原因是datagrid行是根据datagrid ...
- 配置Linux静态IP地址
- Delphi 集合类型
- KVM和Docker的对比
虚拟化技术对比: KVM:全虚拟化,需要模拟各种硬件 docker:严格来说不算是虚拟化技术,只是进程隔离和资源限制 实例启动进程对比: 在kvm虚拟机中执行top命令,看宿主机进程树,根本看不到to ...
- 2019.9.19HTML基础
html:超文本标记语言,不是编程语言,是标签语言,显示数据. 有双标签和单标签 双标签:有开始有结束,<body></body> 单标签:只有一个.<img src=# ...
- hadoop工作流程
一)任务流程 1)Mapreduce程序启动一个Jobclient实例,开启整个mapreduce作业 2)Jobclient通过getnewjobld()j接口向Jobtarker发出请求,以获得一 ...