学习笔记之spring-AOP

来源:互联网 发布:网站生成器软件下载 编辑:程序博客网 时间:2024/05/20 13:05

最近闲来看了一下springaop部分,使用spring来定义纯粹的POJO切面,实现aop,xml如下:

<?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.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">                        <bean id="helloWorldImpl" class="com.kayak.HelloWorldImpl"></bean>        <bean id="timeHandle" class="com.kayak.TimeHandle"></bean>        <aop:config>        <aop:aspect id="time" ref="timeHandle">        <aop:pointcut expression="execution(* com.kayak.HelloWorldImpl.*(..))" id="kayak"/>        <aop:after method="printTime" pointcut-ref="kayak"/>        <aop:before method="printTime" pointcut-ref="kayak"/>        </aop:aspect>        </aop:config>        </beans>

其中定义了一个timeHandle切面类(aspect),helloworldImpl切入点(point-cut),两个adviser提示方法,如果没有事先想IOC容器中注入一个ID为helloWorldImpl的类,就会报错,原因:excution切入点表达式的写法:excution(返回值类型 com.kayak.类名.方法名),这句话的意思是在com.kayak包下面的helloWorldImpl类,如果没有注册,就会报错找不到!

spring-AOP的实现方法还有:使用AspectJ通知注解方式;