JavaWeb学习笔记-spring-16-AOP-@AspectJ配置切面

@AspectJ配置切面

语法基础
  • 方法切点函数
    • execution()
    • @annotation()
  • 方法参数切点函数
    • args()
    • @args()
  • 目标类切点函数
    • within()
    • target()
    • @within()
  • 目标类切点函数
    • @target()
  • 代理类切点函数
    • this()

通配符
* 匹配任意字符,只匹配一个元素
.. 匹配任意字符,匹配多个元素
+ 匹配指定类,必须跟在类后面,匹配本类以及继承和扩展的所有类

  • 支持所有通配符
    • execution()
    • within()
  • 仅支持+通配符
    • args()
    • this()
    • target()
  • 不支持通配符
    • @args()
    • @within()
    • @target()
    • @annotation()

逻辑运算符
&& || !
and or not

实例

@Aspect
public class PreGreetingAspect {
    //增强类型+目标切点表达式
    @Before("execution(* greetTo(..))")
    //增强所有横切逻辑
    public void beforeGreeting(){
        System.out.println("How are you");
    }
}
public class AspectJProxyTest {
    public static void main(String[] args){
        AspectJProxyFactory factory = new AspectJProxyFactory(new NativeWaiter());
        factory.addAspect(PreGreetingAspect.class);
        Waiter proxy = factory.getProxy();
        proxy.greetTo("John");
        proxy.serveTo("Jhon");
    }
}
    <!-- 使用@AspectJ注解切面类-->
    <bean class="com.smart.aspectj.aspectj.PreGreetingAspect"/>
    <!-- 自动代理创建器,将@ApectJ注解切面植入目标Bean中-->
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
<?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">
       <!-- 基于@AspectJ切面的驱动-->
    <aop:aspectj-autoproxy/>
    <bean id="waiter" class="com.smart.NativeWaiter"/>
    <bean class="com.smart.aspectj.aspectj.PreGreetingAspect"/>
</beans>
不同增强类型
  • @Before:前置增强,相当于BeforeAdvice
    • value
    • argNames
  • @AfterReturning:后置增强,相当于AfterReturningAdvice
    • value
    • pointcut
    • returning
    • argNames
  • @Around:环绕增强,相当于MethodInterceptor
    • value
    • argNames
  • @AfterThrowing:抛出增强,相当于ThrowsAdvice
    • value
    • pointcut
    • throwing
    • argNames
  • @After:Final增强
    • value
    • argNames
  • @DeclareParents:引介增强,相当于IntroductionInterceptor
    • value
    • defaultImpl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值