java资料全套\基础+就业\Spring框架2016版视频\day02视频\13-aspectj的aop其他操作.xml

-- 08-aop操作术语(一)




--  Spring中AOP操作的相关术语    http://blog.csdn.net/qq_24693837/article/details/54909870 
Joinpoint(连接点): 类里面可以被增强的方法,这些方法称为连接点
 
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.
 
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
 
Aspect(切面): 是切入点和通知(引介)的结合
 
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象(要增强的类)
Weaving(织入):是把增强应用到目标的过程.
把advice 应用到 target的过程
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类


-- 09-aop操作术语(二) 
 --  Spring中的通知(Advice)和顾问(Advisor) http://www.cnblogs.com/zhangzongle/p/5944906.html 




 
 -- 10-基于 aspecj 的xml准备工作
 -- spring AOP使用AspecJ实现动态代理(亲测success)  http://blog.sina.com.cn/s/blog_acdc06250101mat1.html 
 
 会用就可以了
 
 aspecj 不是spring 的一部分 ,和 spring 一起使用进行aop 操作 , 
 有两种方式 :
 1 :基于 aspecj 的 xml配置 
 2 :基于 aspecj 的注解
 
 
  用浏览器打开 : D:\qudong\spring\spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
 
   
   
   <?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" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->


</beans>






 
 -- 11-使用表达式配置切入点  
 
切入点表达式 execution             http://blog.csdn.net/u012887385/article/details/54600706 
 
参数匹配模式:
()匹配了一个不接受任何参数的方法,
(..)匹配了一个接受任意数量参数的方法(零或者更多)。 
(*)匹配了一个接受一个任何类型的参数的方法。 
(*,String)匹配了一个接受两个参数的方法,第一个可以是任意类型, 第二个则必须是String类型。
 
 任意公共方法的执行:
execution(public * *(..))
任何一个名字以“set”开始的方法的执行:
execution(* set*(..))
AccountService接口定义的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
在service包中定义的任意方法的执行:
execution(* com.xyz.service.*.*(..))
在service包或其子包中定义的任意方法的执行:
execution(* com.xyz.service..*.*(..))
在service包中的任意连接点(在Spring AOP中只是方法执行):
within(com.xyz.service.*)
在service包或其子包中的任意连接点(在Spring AOP中只是方法执行):
within(com.xyz.service..*)
实现了AccountService接口的代理对象的任意连接点 (在Spring AOP中只是方法执行):
this(com.xyz.service.AccountService)
'this'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得代理对象在通知体内可用。
 
 
 
 
 常用的表达式 : execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
execution(* cn.itcast.aop.Book.add(..)) : add方法进行增强 
execution(* cn.itcast.aop.Book.add(..)) : Book类所有方法进行增强 
execution(* *.*(..)) : 所有方法进行增强 
 
 
 
 -- 12-aspectj的aop操作  
 
 Spring 之AOP AspectJ切入点语法详解(最全面、最详细。)  http://blog.csdn.net/zhengchao1991/article/details/53391244 
 --  被增强的类
package cn.itcast.aop;


public class Book {


public void add() {
System.out.println("add...........");
}
}
--  增强的类
package cn.itcast.aop;


import org.aspectj.lang.ProceedingJoinPoint;


public class MyBook {


public void before1() {
System.out.println("前置增强......");
}

public void after1() {
System.out.println("后置增强......");
}

//环绕通知
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//方法之前
System.out.println("方法之前.....");

//执行被增强的方法
proceedingJoinPoint.proceed();

//方法之后
System.out.println("方法之后.....");
}
}






 -- /spring_day02_demo/src/bean3.xml
  <!-- 1  配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>

 
 <!-- 2 配置aop操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1"/>

<!-- 2.2 配置切面 
把增强用到方法上面
-->
<aop:aspect ref="myBook">
<!-- 配置增强类型 
method: 增强类里面使用哪个方法作为前置
-->
<aop:before method="before1" pointcut-ref="pointcut1"/>

<aop:after-returning method="after1" pointcut-ref="pointcut1"/>

<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>

 
 -- 
 package cn.itcast.aop;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestAnno {

@Test
public void testService() {
ApplicationContext context = 
new ClassPathXmlApplicationContext("bean3.xml");
Book book = (Book) context.getBean("book");
book.add();
}
}


 
 
-- 13-aspectj的aop其他操作 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值