jQuery Aop 简明教程

在jQuery中添加AOP的功能,可以使用jquery plugin.[url]http://code.google.com/p/jquery-aop/[/url]。使用非常简单,包含.js文件,然后调用添加通知的函数。
一共有四种通知:前置通知,后置通知,环绕通知和引入。

前置通知:before (Map pointcut, Function advice) return Array<Function>
在指定织入点创建一个前置通知。通知在被织入的方法之前执行,不能改变原方法的行为或阻止它执行。

参数:
pointcut: 织入点对象
target:被织入的对象
method:被织入的方法名字
advice: 通知函数

例:
jQuery.aop.before( {target: window, method: 'MyGlobalMethod'}, 
function() {
alert('About to execute MyGlobalMethod');
}
);

jQuery.aop.before( {target: window, method: /My/},
function() {
alert('About to execute one of my global methods');
}
);

jQuery.aop.before( {target: String, method: 'indexOf'},
function(index) {
alert('About to execute String.indexOf on: ' + this);
}
);


后置通知:after (Map pointcut, Function advice) return Array<Function>
通知(advice)在定义的切入点后面执行(pointcut),并接收切入点方法运行后的返回值作为参数


参数:
pointcut: 织入点对象
target:被织入的对象
method:被织入的方法名字。
advice: 通知函数,并接受切入点方法执行后的返回值作为参数

例:
jQuery.aop.after( {target: window, method: 'MyGlobalMethod'}, 
function(result) {
alert('Returned: ' + result);
}
);

jQuery.aop.after( {target: String, method: 'indexOf'},
function(index) {
alert('Result found at: ' + index + ' on:' + this);
}
);



环绕通知:around (Map pointcut, Function advice) return Array<Function>
在指定切入点处创建一个环绕通知,此类型的同志通过调用innovation.proceed()能够控制切入点方法的执行,也能在函数执行前更改它的参数。

参数:
pointcut: 织入点对象
target:被织入的对象
method:被织入的方法名字。
advice: 通知函数,有一个参数innovation。包含.proceed()方法和两个属性:.argurments及.method

例:
jQuery.aop.around( {target: window, method: 'MyGlobalMethod'}, 
function(invocation) {
alert('# of Arguments: ' + invocation.arguments.length);
return invocation.proceed();
}
);

jQuery.aop.around( {target: String, method: 'indexOf'},
function(invocation) {
alert('Searching: ' + invocation.arguments[0] + ' on: ' + this);
return invocation.proceed();
}
);

jQuery.aop.around( {target: window, method: /Get(\d+)/},
function(invocation) {
alert('Executing method ' + invocation.method);
return invocation.proceed();
}
);



引入:introduction (Map pointcut, Function advice) return Array<Function>
此类型的通知的方法(advice)将替代制定切入点的方法。要恢复原方法,唯有卸载通知。

pointcut: 织入点对象
target:被织入的对象
method:被织入的方法名字。
advice: 通知函数。

例:
jQuery.aop.introduction( {target: String, method: 'log'}, 
function() {
alert('Console: ' + this);
}
);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot AOP(面向切面编程)是一种在Spring Boot应用中实现横切关注点的技术。它允许开发者在应用的多个地方插入额外的逻辑,而不需要修改原始代码。下面是一个简单的Spring Boot AOP教程: 1. 添加依赖:在你的Spring Boot项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 创建切面类:创建一个Java类,并使用`@Aspect`注解标记它作为一个切面类。在切面类中,你可以定义各种通知(advice)和切点(pointcut)。例如: ```java @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("After returning method: " + joinPoint.getSignature().getName()); System.out.println("Result: " + result); } // 其他通知和切点的定义... } ``` 在上面的例子中,`logBefore()`方法是一个在目标方法执行之前执行的前置通知(Before Advice),`logAfterReturning()`方法是一个在目标方法执行之后执行的后置通知(After Returning Advice)。 3. 启用AOP:在Spring Boot应用的主类上添加`@EnableAspectJAutoProxy`注解,以启用AOP: ```java @SpringBootApplication @EnableAspectJAutoProxy public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 4. 测试AOP:运行应用,并调用被切入的方法,你将看到相关的日志输出。 这只是一个简单的Spring Boot AOP教程AOP还有很多其他的功能和概念,例如切面优先级、环绕通知等等。你可以进一步学习和探索Spring Boot AOP的更多功能和用法。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值