学习记录

来源:互联网 发布:网站源码有什么用 编辑:程序博客网 时间:2024/06/14 16:43

@AspectJ的风格类似纯java注解的普通java类

Spring可以使用AspectJ来做切入点解析

AOP的运行时仍旧是纯的Spring AOP,对AspectJ的编译器或者织入无依赖性


@Aspect注解是不能够通过类路径自动检测发现的,所以需要配合使用@Component注释或者在xml配置bean

一个类中的@Aspect注解标识它为一个切面,并且将自己从自动代理中排除


@Pointcut注解,方法返回类型必须为void


Supported Pointcut Designators

execution 匹配方法执行的连接点

within 限定匹配特定类型的连接点

this 匹配特定链接点的bean引用是指定类型的实例的限制

target 限定匹配特点链接点的目标对象参数是指定类型的实例

args 限定匹配特点链接点的参数是给定类型的实例

@target 限定匹配特点链接点的类执行对象的具有给定类型的注解

@args 限定匹配特定连接点实际传入参数的类型具有给定类型的注解

@within 限定匹配到内具有给定的注释类型的连接点

@annotation 限定匹配特定连接点的主体具有给定的注解


示例

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: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.imooc.aop.aspectj"/>     <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>


Java

@Component@Aspectpublic class MoocAspect {@Pointcut("execution(* com.imooc.aop.aspectj.biz.*Biz.*(..))")public void pointcut() {}@Pointcut("within(com.imooc.aop.aspectj.biz.*)")public void bizPointcut() {}}

定义良好的pointcuts

AspectJ是编译期的AOP

检查代码并匹配连接点与切入点的代价是昂贵的

一个好的切入点应该包括以下几点:

   选择特定类型的连接点,如:execution,get,set,call,handler

   确定连接点范围,如:within,withcode

   匹配上下文信息,如:this,target,@annotation



0 0