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

举报
小工匠 发表于 2021/09/10 01:35:48 2021/09/10
【摘要】 概述语法实例 withincomxgjNaiveWaiterwithincomxgjwithincomxgj withincomxgjMark 概述 通过类匹配模...

概述

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

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

语法

within(<类匹配模式>)
  
 
  • 1

比如 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);
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

注解标注的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);
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
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);

    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

增强切面

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");
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

配置文件

<?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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

测试类

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");

    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

运行结果:

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

可以看到,只有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");
    }

}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

再此运行

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
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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");
    }
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

测试类获取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");
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

运行结果:

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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


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");
    }
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

再此运行

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


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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


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 "";

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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);

    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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);

    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

修改切面

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

运行测试类

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


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/77790488

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。