Spring-AOP @AspectJ切点函数之within()

概述

通过类匹配模式串声明切点,within()函数定义的连接点是针对目标类而言的,而非针对运行期对象的类型而言,这一点和execution()是相同的。

但是within()和execution()函数不同的是,within()所指定的连接点最小范围只能是类,而execution()所指定的连接点可以大到包,小到方法入参。 所以从某种意义上讲,execution()函数功能涵盖了within()函数的功能

语法

within(<类匹配模式>)

比如 within(com.xgj.NaiveWaiter),是within()函数能表达的最小粒度。

用法举例:

  • within(com.xgj.NaiveWaiter) 匹配目标类NaiveWaiter的所有方法。 如果切点调整为within(com.xgj.Waiter),则NaiveWaiter和NaughtyWaiter中的所有方法都不匹配。 而Waiter本身是接口,不可能实例化,所以within(com.xgj.Waiter)的声明是无意义的。

  • within(com.xgj.*) 匹配com.xgj包中的所有类的方法,但是不包含子孙包中类的方法

  • within(com.xgj..*)匹配com.xgj包以及子孙包中的所有类的方法都匹配这个切点

  • within(@com.xgj.Mark *) 匹配com.xgj及子包下带有@com.xgj.Mark 注解的任何类(接口不行)的任何方法


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster


within(com.xgj.NaiveWaiter)

这里写图片描述

接口类

package com.xgj.aop.spring.advisor.aspectJ.function.within;

public interface Waiter {

    void greetTo(String clientName);

    void serverTo(String clientName);
}

注解标注的2个POJO

package com.xgj.aop.spring.advisor.aspectJ.function.within;

import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @ClassName: NaughtyWaiter
 * 
 * @Description: @Component标注的bean
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月5日 上午1:31:10
 */

@Component
public class NaughtyWaiter implements Waiter {

    @Override
    public void greetTo(String clientName) {
        System.out.println("NaughtyWaiter greetTo " + clientName);
    }

    @Override
    public void serverTo(String clientName) {
        System.out.println("NaughtyWaiter greetTo " + clientName);
    }

}
package com.xgj.aop.spring.advisor.aspectJ.function.within;

import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @ClassName: NaiveWaiter
 * 
 * @Description: @Component标注的Bean
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月5日 上午1:30:52
 */

@Component
public class NaiveWaiter implements Waiter {

    @Override
    public void greetTo(String clientName) {
        System.out.println("NaiveWaiter greetTo " + clientName);
    }

    @Override
    public void serverTo(String clientName) {
        System.out.println("NaiveWaiter serverTo " + clientName);

    }

}

增强切面

package com.xgj.aop.spring.advisor.aspectJ.function.within;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

/**
 * 
 * 
 * @ClassName: WithinAspect
 * 
 * @Description: 标注了@Aspect的切面
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月5日 上午1:21:17
 */

@Aspect
public class WithinAspect {

    @AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within.NaiveWaiter)")
    public void crossCuttingCode() {
        System.out.println("后置增强 some logic is here\n");
    }

}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

<!-- (1)声明Context命名空间以及Schema文件   (2)扫描类包以及应用注解定义的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.within"/>

<!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.within.WithinAspect"/>

</beans>

测试类

package com.xgj.aop.spring.advisor.aspectJ.function.within;

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

public class WithinAspectTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml");

        NaiveWaiter naiveWaiter = ctx.getBean("naiveWaiter", NaiveWaiter.class);
        naiveWaiter.greetTo("XiaoGongJiang");
        naiveWaiter.serverTo("XiaoGongJiang");

        NaughtyWaiter naughtyWaiter = ctx.getBean("naughtyWaiter",
                NaughtyWaiter.class);
        naughtyWaiter.greetTo("XiaoGongJiang");
        naughtyWaiter.serverTo("XiaoGongJiang");

    }
}

运行结果:

2017-09-05 01:32:21,687  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24b9371e: startup date [Tue Sep 05 01:32:21 BOT 2017]; root of context hierarchy
2017-09-05 01:32:21,786  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml]
NaiveWaiter greetTo XiaoGongJiang
后置增强 some logic is here

NaiveWaiter serverTo XiaoGongJiang
后置增强 some logic is here

NaughtyWaiter greetTo XiaoGongJiang
NaughtyWaiter greetTo XiaoGongJiang

可以看到,只有NaiveWaiter类被织入了横切逻辑。


如果我们将切面中的within函数改为within(com.xgj.aop.spring.advisor.aspectJ.function.within.Waiter)

@Aspect
public class WithinAspect {

    @AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within.Waiter)")
    public void crossCuttingCode() {
        System.out.println("后置增强 some logic is here\n");
    }

}

再此运行

2017-09-05 01:33:27,989  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61ee30d2: startup date [Tue Sep 05 01:33:27 BOT 2017]; root of context hierarchy
2017-09-05 01:33:28,066  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml]
NaiveWaiter greetTo XiaoGongJiang
NaiveWaiter serverTo XiaoGongJiang
NaughtyWaiter greetTo XiaoGongJiang
NaughtyWaiter greetTo XiaoGongJiang

within(com.xgj.*)

先增加一个子目录seller, 然后 改造下 切面类

这里写图片描述

@Aspect
public class WithinAspect {

    // 匹配com.xgj.aop.spring.advisor.aspectJ.function.within包下的所有类的所有方法,不包括子孙包
    @AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within.*)")
    public void crossCuttingCode() {
        System.out.println("后置增强 some logic is here\n");
    }
}

测试类获取SmartSeller,然后调用目标方法

package com.xgj.aop.spring.advisor.aspectJ.function.within;

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

import com.xgj.aop.spring.advisor.aspectJ.function.within.seller.SmartSeller;

public class WithinAspectTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml");

        NaiveWaiter naiveWaiter = ctx.getBean("naiveWaiter", NaiveWaiter.class);
        naiveWaiter.greetTo("XiaoGongJiang");
        naiveWaiter.serverTo("XiaoGongJiang");

        NaughtyWaiter naughtyWaiter = ctx.getBean("naughtyWaiter",
                NaughtyWaiter.class);
        naughtyWaiter.greetTo("XiaoGongJiang");
        naughtyWaiter.serverTo("XiaoGongJiang");

        SmartSeller smartSeller = ctx.getBean("smartSeller", SmartSeller.class);
        smartSeller.smileTo("XiaoGongJiang");
        smartSeller.jokeTo("XiaoGongJiang");
    }
}

运行结果:

2017-09-05 01:39:44,352  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a06514b: startup date [Tue Sep 05 01:39:44 BOT 2017]; root of context hierarchy
2017-09-05 01:39:44,433  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml]
NaiveWaiter greetTo XiaoGongJiang
后置增强 some logic is here

NaiveWaiter serverTo XiaoGongJiang
后置增强 some logic is here

NaughtyWaiter greetTo XiaoGongJiang
后置增强 some logic is here

NaughtyWaiter greetTo XiaoGongJiang
后置增强 some logic is here

SmartSeller serverTo XiaoGongJiang
SmartSeller greetTo XiaoGongJiang

可以看到,只有当前目录下的所有类的方法被织入了横切逻辑,而子孙包中的没有被织入增强。


within(com.xgj..*)

改造下切面类

@Aspect
public class WithinAspect {

    // 匹配com.xgj.aop.spring.advisor.aspectJ.function.within包下的所有类的所有方法,包括子孙包
    @AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within..*)")
    public void crossCuttingCode() {
        System.out.println("后置增强 some logic is here\n");
    }
}

再此运行

2017-09-05 01:42:56,488  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a06514b: startup date [Tue Sep 05 01:42:56 BOT 2017]; root of context hierarchy
2017-09-05 01:42:56,599  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml]
NaiveWaiter greetTo XiaoGongJiang
后置增强 some logic is here

NaiveWaiter serverTo XiaoGongJiang
后置增强 some logic is here

NaughtyWaiter greetTo XiaoGongJiang
后置增强 some logic is here

NaughtyWaiter greetTo XiaoGongJiang
后置增强 some logic is here

SmartSeller serverTo XiaoGongJiang
后置增强 some logic is here

SmartSeller greetTo XiaoGongJiang
后置增强 some logic is here

可以看到,子孙包中的所有类的所有方法都能被织入了增强.


within(@com.xgj.Mark *)

增加一个自定义的注解,或者使用框架自带的注解 都可以,用于测试

package com.xgj.aop.spring.advisor.aspectJ.function.within;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 
 * 
 * @ClassName: Mart
 * 
 * @Description: 自定义注解
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月5日 下午12:02:46
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Mart {

    public String value() default "";

}

NaiveWaiter类标注@Mart

package com.xgj.aop.spring.advisor.aspectJ.function.within;

import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @ClassName: NaiveWaiter
 * 
 * @Description: @Component标注的Bean
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月5日 上午1:30:52
 */

@Mart
@Component
public class NaiveWaiter implements Waiter {

    @Override
    public void greetTo(String clientName) {
        System.out.println("NaiveWaiter greetTo " + clientName);
    }

    @Override
    public void serverTo(String clientName) {
        System.out.println("NaiveWaiter serverTo " + clientName);

    }

}

NaughtyWaiter没有类标注@Mart

子目录 SmartSeller 标注 @Mart

package com.xgj.aop.spring.advisor.aspectJ.function.within.seller;

import org.springframework.stereotype.Component;

import com.xgj.aop.spring.advisor.aspectJ.function.within.Mart;

/**
 * 
 * 
 * @ClassName: SmartSeller
 * 
 * @Description: @Component标注的Bean
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月5日 上午1:30:52
 */

@Mart
@Component
public class SmartSeller {

    public void jokeTo(String clientName) {
        System.out.println("SmartSeller greetTo " + clientName);
    }

    public void smileTo(String clientName) {
        System.out.println("SmartSeller serverTo " + clientName);

    }

}

修改切面

@AfterReturning("within(@com.xgj.aop.spring.advisor.aspectJ.function.within.Mart *)")
    public void crossCuttingCode() {
        System.out.println("后置增强 some logic is here\n");
    }

运行测试类

2017-09-05 17:50:54,071  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@54397c28: startup date [Tue Sep 05 17:50:54 BOT 2017]; root of context hierarchy
2017-09-05 17:50:54,179  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml]
NaiveWaiter greetTo XiaoGongJiang
后置增强 some logic is here

NaiveWaiter serverTo XiaoGongJiang
后置增强 some logic is here

NaughtyWaiter greetTo XiaoGongJiang
NaughtyWaiter greetTo XiaoGongJiang
SmartSeller serverTo XiaoGongJiang
后置增强 some logic is here

SmartSeller greetTo XiaoGongJiang
后置增强 some logic is here

匹配com.xgj.aop.spring.advisor.aspectJ.function.within及子包下带有@com.xgj.aop.spring.advisor.aspectJ.function.within.Mark 注解的任何类(接口不行)的任何方法

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值