AspectJ 切点指示器-06-within和@within

我怕爱的太早我们不能终老 提交于 2020-02-26 00:57:47
target() 限制连接点匹配目标对象为指定类型的类
@target() 限制连接点匹配目标对象为被特定注解标注的类

within()与execution()的功能类似,两者的区别是,within()定义的连接点的最小范围是类级别的(不是接口),而execution()定义的连接点的最小范围可以精确到方法的入参,因此可以认为execution()涵盖了within()的功能。

“@within()”匹配标注了指定注解的类及其子孙类。

最终类结构图

1、Factory

package com.test.aspectj.expression;

/**
 * 工厂接口
 */
public interface Factory {

    // 制作产品
    void make();

    // 运输
    void delivery(String address);
}

 2、PhoneFactory

package com.test.aspectj.expression;

import com.test.aspectj.expression.annotation.Log;
import org.springframework.stereotype.Component;

/**
 * 手机工厂
 */
@Component
public class PhoneFactory implements Factory {

    // 制作产品的方法
    @Override
    @Log
    public void make() {
        System.out.println("来自目标类PhoneFactory的消息:生产手机");
    }

    // 运输手机的方法
    @Override
    public void delivery(String address) {
        System.out.println("来自目标类PhoneFactory的消息:运输手机至 " + address);
    }

    // 测试@Within注解
    public void testWithin() {
    }
 }

3、MobilePhoneFactory

package com.test.aspectj.expression.within;

import com.test.aspectj.expression.PhoneFactory;
import org.springframework.stereotype.Component;

/**
 * 手机工厂
 */
@Monitor
@Component
public class MobilePhoneFactory extends PhoneFactory {
    @Override
    public void testWithin() {

    }
}

4、IPhoneFactory

package com.test.aspectj.expression.within;

import org.springframework.stereotype.Component;

/**
 * iPhone手机工厂
 */
@Component(value = "iPhoneFactory")
public class IPhoneFactory extends MobilePhoneFactory {

}

5、自定义注解@Monitor

package com.test.aspectj.expression.within;

import java.lang.annotation.*;

/**
 * 监控
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
public @interface Monitor {
    String value() default "";
}

6、切面 WithinAspect

package com.test.aspectj.expression.within;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 测试within()和@ within()的切面
 */
@Aspect
public class WithinAspect {

    /**
     * FoodFactory下的方法执行会增强
     */
    @Before("within(com.test.aspectj.expression.FoodFactory)")
    public void before() {
        System.out.println("来自切面里的消息:within匹配到,方法执行前增强");
    }

    /**
     * 类上有@Monitor注解,这个类以及它的子孙类都能被增强
     */
    @After("@within(com.test.aspectj.expression.within.Monitor)")
    public void after() {
        System.out.println("来自切面里的消息:@within匹配到,执行增强");
    }
}

7、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:aop="http://www.springframework.org/schema/aop"
       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">
    
    <context:component-scan base-package="com.test.aspectj.expression"/>
    <bean id="annotationAspect" class="com.test.aspectj.expression.within.WithinAspect"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

8、测试代码

package com.test.aspectj.expression.within;

import com.test.aspectj.expression.Factory;
import com.test.aspectj.expression.PhoneFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试 within 和 @within
 */
public class WithinExpressionDemo {
    public static void main(String[] args) {
        System.out.println("=============== 测试 within ===============");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjwithinexpression.xml");
        Factory foodFactory = (Factory) context.getBean("foodFactory");
        foodFactory.make();
        System.out.println("-----我是分割线-----");
        foodFactory.delivery("北京");
        System.out.println("=============== 测试 @within ===============");
        System.out.println("-----MobilePhoneFactory上有注解@Monitor-----");
        MobilePhoneFactory mobilePhoneFactory = (MobilePhoneFactory) context.getBean("mobilePhoneFactory");
        mobilePhoneFactory.testWithin();
        System.out.println("-----IPhoneFactory的父类MobilePhoneFactory上有注解@Monitor-----");
        IPhoneFactory iPhoneFactory = (IPhoneFactory) context.getBean("iPhoneFactory");
        iPhoneFactory.testWithin();
    }
}

9、运行结果

=============== 测试 within ===============
来自切面里的消息:within匹配到,方法执行前增强
来自目标类FoodFactory的消息:生产食品
-----我是分割线-----
来自切面里的消息:within匹配到,方法执行前增强
来自目标类FoodFactory的消息:销售食品至 北京
=============== 测试 @within ===============
-----MobilePhoneFactory上有注解@Monitor-----
来自切面里的消息:@within匹配到,执行增强
-----IPhoneFactory的父类MobilePhoneFactory上有注解@Monitor-----
来自切面里的消息:@within匹配到,执行增强

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!