Spring AOP 基本的使用
1. jar包
2.全局配置文件
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
3.1使用bean 的方式配置
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="before" class="com.aspect.Before"></bean>
<bean id="tests" class="com.test.Tests"> </bean>
<bean id="after" class="com.aspect.After"></bean>
<bean id="final" class="com.aspect.Final"></bean>
<bean id="exception" class="com.aspect.Exception"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.test.Tests.say(*))" id="say"/><!-- 切入点 -->
<aop:pointcut expression="execution(* com.test.Test.say1(*))" id="say1"></aop:pointcut>
<aop:aspect ref="before"> <!-- 切入的方式(前后环异常最终)、方法 -->
<aop:before method="dobefore" pointcut-ref="say"/>
</aop:aspect>
<aop:aspect ref="after">
<aop:after-returning method="doafter" pointcut-ref="say" returning="obj"/>
</aop:aspect>
<aop:aspect ref="final">
<aop:after method="dofinal" pointcut-ref="say"/>
</aop:aspect>
<!-- <aop:aspect ref="exception">
<aop:after-throwing method="doexception" pointcut-ref="say" throwing="e"/>
</aop:aspect> -->
</aop:config>
</beans>
在java中创建对应的 切入方法
3.2 使用注解配置
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.aspect.auto"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
创建对应的切入方法,并使用注解:如:
package com.aspect.auto;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Around {
@org.aspectj.lang.annotation.Around("within(com.aspect.auto.Says)")
public void doAround(ProceedingJoinPoint jp){
try {
System.out.println("前置通知");
jp.proceed();
System.out.println(jp.proceed());
System.out.println("后置通知");
} catch (Throwable e) {
System.out.println("异常通知:"+e.getMessage());
}
System.out.println("最终通知");
}
}
package com.aspect.auto;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class After {
@AfterReturning(pointcut="execution(* com..*.say(*))",returning="ret")
public void after(JoinPoint jp,Object ret){
System.out.println("执行后置通知");
System.out.println("获得返回值:"+ret);
}
}
测试 end
Spring AOP 基本的使用的更多相关文章
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
- spring aop
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- 基于Spring AOP的JDK动态代理和CGLIB代理
一.AOP的概念 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...
- Spring AOP详解
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- Spring AOP实例——异常处理和记录程序执行时间
实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...
- 从零开始学 Java - Spring AOP 实现用户权限验证
每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...
- 从零开始学 Java - Spring AOP 实现主从读写分离
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
- 从零开始学 Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
随机推荐
- Mac的VIM中delete键失效的原因和解决方案
在Mac的键盘上实际是没有backspace这个键的.其实Mac的delete就是Windows的backspace,实现的都是向左删除的功能.Mac上如果要实现向右删除的功能需要使用⌘+delete ...
- 清除DNS解析缓存
接下来在弹出的命令提示符窗口中输入“ipconfig /displaydns”,我们会看到系统中有多条我们之前使用过的DNS地址,如下图所示 5 然后,我们接着输入命令“ipconfig /flush ...
- Java枚举类型enum使用详解
java的Enum枚举类型终于在j2se1.5出现了.之前觉得它只不过是鸡肋而已,可有可无.毕竟这么多年来,没有它,大家不都过得很好吗?今日看<Thinking in Java>4th ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:图片响应式 (将很好地扩展到父元素)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- springboot内嵌定时任务使用及cron表达式讲解
第一步:pom引入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- 使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整
安装 略. 转码 最简单命令如下: ffmpeg -i out.ogv -vcodec h264 out.mp4ffmpeg -i out.ogv -vcodec mpeg4 out.mp4ffmpe ...
- vscode spring boot配置文件application.properties不提示解决方式
背景 因实际的编程环境是jdk1.6,vscode安装了spring boot tools开发后,application.properties无法提示.spring boot tools的功能之一就是 ...
- centos7下yourcompleteme安装
以前装过一回,没成功,现在再来一次 yourcompleteme git https://github.com/ycm-core/YouCompleteMe#installation 检查软件版本 v ...
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
- leetcode1 twoSum
""" Given an array of integers, return indices of the two numbers such that they add ...