必威体育Betway必威体育官网
当前位置:首页 > IT技术

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

时间:2019-07-30 04:11:06来源:IT技术作者:seo实验室小编阅读:77次「手机版」
 

within

  • 概述
  • 语法
  • 实例
    • withincomxgjNaiveWaiter
    • withincomxgj
    • withincomxgj
  • withincomxgjMark

概述

通过类匹配模式串声明切点,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 注解的任何类(接口不行)的任何方法

相关阅读

随机函数JAVA

首先第一步是创建Java 接下来开始我们随机函数的代码块 package first; import java.util.StringTokenizer; public class First

Excel中使用Match函数匹配查找指定内容的操作方法

在EXCEL中使用INDEX函数来定位查找数据,其中还配合使用了Match函数,今天,seo实验室小编就教大家在Excel中使用Match函数匹配查找指定

【C++笔记】回调函数

http://www.cnblogs.com/ioleon13/archive/2010/03/02/1676621.html https://blog.csdn.net/HouQi02/article/details/52205311

python list的append 函数

x = [1, 2, 3] y = [] y.append(x) x.append(9) y.append(x) print(y)期望结果是 [[1, 2, 3], [1, 2, 3, 9]]实际的输出结果是 [

math.pow()函数用法

Math.pow(底数,几次方)如:int a=3;        int b=3; int c=Math.pow(a,b);  就是3的三次方是多少;   c最终为27; 基础用

分享到:

栏目导航

推荐阅读

热门阅读