AspectJ 切点指示器-04-target和@target

房东的猫 提交于 2020-02-25 21:04:58
target() 匹配目标类以及子类,目标类以及其子类的所有方法执行都会匹配到
@target() 匹配目标对象被特定注解标注的类

最终类结构图

1、Factory

package com.test.aspectj.expression;

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

    // 制作产品
    void make();

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

2、PhoneFactory,会被TargetAspect里定义的@Before("target(com.test.aspectj.expression.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、HuaweiPhoneFactory,是PhoneFactory的子类,会被TargetAspect里定义的@Before("target(com.test.aspectj.expression.PhoneFactory)")匹配到;有@Run自定义注解,会被TargetAspect里定义的@After("@target(com.test.aspectj.expression.target.Run)")匹配到

package com.test.aspectj.expression.target;

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

/**
 * 华为手机工厂
 */
@Run
@Component
public class HuaweiPhoneFactory extends PhoneFactory {
}

4、Run 自定义注解

package com.test.aspectj.expression.target;

import java.lang.annotation.*;

/**
 * 执行的注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
public @interface Run {
    String value() default "";
}

5、切面类 TargetAspect

package com.test.aspectj.expression.target;

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

/**
 * target 和 @target 测试
 */
@Aspect
public class TargetAspect {

    /**
     * target() 表示目标类型是指定的类型时,目标类型以及子孙类中的的所有方法都能匹配到
     *
     * 本实例就是 PhoneFactory、以及子类 HuaweiPhoneFactory 的所有的方法都能匹配到
     */
    @Before("target(com.test.aspectj.expression.PhoneFactory)")
    public void before() {
        System.out.println("来自切面的消息:target 匹配到,方法执行前增强");
    }

    /**
     * @taret() 匹配标注了指定注解的类
     *
     * HuaweiPhoneFactory 有@Run注解,因此 HuaweiPhoneFactory 所有方法都会匹配到
     * PhoneFactory 没有@Run注解,因此 PhoneFactory 所有方法都不会匹配到
     */
    @After("@target(com.test.aspectj.expression.target.Run)")
    public void after() {
        System.out.println("来自切面的消息:@target 匹配到,方法执行后增强");
    }
}

 6、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.target.TargetAspect"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

7、测试代码

package com.test.aspectj.expression.target;

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

/**
 * target 和 @target 测试
 */
public class TargetExpressionDemo {
    public static void main(String[] args) {
        System.out.println("=============== 测试 target 和 @target ===============");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjtargetexpression.xml");
        System.out.println("-----PhoneFactory被增强-----");
        PhoneFactory phoneFactory = (PhoneFactory) context.getBean("phoneFactory");
        phoneFactory.make();
        System.out.println("-----PhoneFactory的子类HuaweiPhoneFactory也能被增强-----");
        HuaweiPhoneFactory huaweiPhoneFactory = (HuaweiPhoneFactory) context.getBean("huaweiPhoneFactory");
        huaweiPhoneFactory.make();
    }
}

 8、运行结果

=============== 测试 target 和 @target ===============
-----PhoneFactory被增强-----
来自切面的消息:target 匹配到,方法执行前增强
来自目标类PhoneFactory的消息:生产手机
-----PhoneFactory的子类HuaweiPhoneFactory也能被增强-----
来自切面的消息:target 匹配到,方法执行前增强
来自目标类PhoneFactory的消息:生产手机
来自切面的消息:@target 匹配到,方法执行后增强

 

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