java around_基于Annotation的Spring AOP: @Around | 学步园
@Around 增强处理是功能比较强大的增强处理,它近似等于Before 和AfterReturning的总和。@Around既可在执行目标方法之前织入增强动作,也可在执行目标方法之后织入增强动作。@Around甚至可以决定目标方法在什么时候执行,如何执行,更甚者可以完全阻止目标方法的执行。
@Around可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回值。
@Around功能虽然强大,但通常需要在线程安全的环境下使用。因此,如果使用普通的Before、AfterReturning就能解决的问题,就没有必要使用Around了。如果需要目标方法执行之前和之后共享某种状态数据,则应该考虑使用Around。尤其是需要使用增强处理阻止目标的执行,或需要改变目标方法的返回值时,则只能使用Around增强处理了。
当定义一个Around增强处理方法时,该方法的第一个形参必须是ProceedingJoinPoint 类型,在增强处理方法体内,调用ProceedingJoinPoint的proceed方法才会执行目标方法------这就是@Around增强处理可以完全控制目标方法执行时机、如何执行的关键;如果程序没有调用ProceedingJoinPoint的proceed方法,则目标方法不会执行。
调用ProceedingJoinPoint的proceed方法时,还可以传入一个Object[ ]对象,该数组中的值将被传入目标方法作为实参。如果传入的Object[ ]数组长度与目标方法所需要的参数个数不相等,或者Object[ ]数组元素与目标方法所需参数的类型不匹配,程序就会出现异常。
Person.java :
public interface Person {
public String sayHello(String name);
public void eat(String food);
public void divide();
}
Chinese.java :
@Component
public class Chinese implements Person {
@Override
public void divide() {
int a=5/0;
System.out.println("divide执行完成!");
}
@Override
public String sayHello(String name) {
System.out.println("sayHello方法被调用...");
return name+" Hello,Spring AOP";
}
@Override
public void eat(String food) {
System.out.println("我正在吃:"+food);
}
}
AroundAdviceTest.java :
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AroundAdviceTest {
@Around("execution(* com.bean.*.*(..))")
public Object processTx(ProceedingJoinPoint jp) throws Throwable{
System.out.println("执行目标方法之前,模拟开始事务...");
Object rvt=jp.proceed(new String[]{"被改变的参数"});
System.out.println("执行目标方法之后,模拟结束事务...");
return rvt+"新增的内容";
}
}
bean.xml :
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
expression="org.aspectj.lang.annotation.Aspect"/>
Test.java :
public class Test {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
Person p=(Person) ctx.getBean("chinese");
System.out.println(p.sayHello("张三"));
p.eat("西瓜");
p.divide();
}
}
运行程序,控制台输出:
java around_基于Annotation的Spring AOP: @Around | 学步园相关推荐
- 基于@AspectJ配置Spring AOP之一--转
原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...
- 基于注解的Spring AOP的配置和使用--转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- java oval_Java对象校验框架Oval怎么使用 | 学步园
OVal是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件.Annotation.POJOs进行设定.可以使用纯Java语言.JavaScript.Groovy.BeanShell等进 ...
- java有关地址的讲解_Java迭代器讲解 | 学步园
迭代器在其实就是指针,读取集合或者数组中的一个值,读完以后又指向下一条数据. iterator() 迭代器只读,不能改效率要比for循环高 迭代器的一些方法: HasNext() 如果仍有元素可以迭代 ...
- Spring学习笔记之二----基于XML的Spring AOP配置
在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...
- osgi java web_基于OSGi和Spring开发企业级Web应用
Spring 作为一个新的事实上的工业标准,OSGi已经受到了广泛的关注,就在不久前EclipseCon也发布企业级OSGi标准,而IBM以及Eclipse也宣称将大力发展Java模块化.Spring ...
- java spring context_java将对象注册到spring context中 | 学步园
很多时候需要从spring中取出相应的对象,很多时候又需要将对象注入到spring context中. 小卒碰到了这样的问题.在构件中有DAO. 其他构件需要复用这个DAO,但是连接配置各不相同,并且 ...
- java oauth2 severlet_【Servlet】基于Jsp的微信Oauth2认证 | 学步园
挂载到微信服务器上的应用程序,能够通过微信Oauth2认证,能够抓取到用户的微信信息,当然,你首先要通过微信的帐号资质审核. 一.基本思想 二.基本过程 1.登陆微信的公众平台(点击打开链接),在左侧 ...
- java bean命名规范_JavaBean 成员命名规范 | 学步园
在eclipse中自动生成setters()和getters() User.java private boolean active ; //getter() public boolean isActi ...
最新文章
- iPhone曝严重漏洞,用户接听FaceTime前或被“监听”!
- android Fragment 学习资料推荐
- 利用XML实现通用WEB报表打印(转载)
- excel取整函数_Excel中的这些烧脑问题,你遇到过几种?
- win8.1和wp8.1共用代码,需要注意的一些问题
- 委托、Lambda表达式、事件系列07,使用EventHandler委托
- azure服务器_如何使用Blazor WebAssembly实施Azure无服务器
- [网络编程] - MIME格式详细介绍[转]
- Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy
- 标准库中迭代器的关系
- 诗意的边缘(PHP顶级框架Zend Fr
- WCF读书笔记(1)
- 2021-09-07LRUCache机制
- OpenCore 启动菜单界面美化增加gui界面
- 技术分享 | 相较于ROS 1,ROS 2有哪些优越性?
- UOJ220 [NOI2016] 网格 【割顶】【并查集】
- 什么是SLA?SLA服务水平深度解析
- Long与Integer的转换
- Java8 新特性之流式数据处理
- 社会学与计算机哪个考研容易,国内几所较热的社会学系考研难度比较