AspectJ pointcut indicators - 04 target and @ target

target() Match all method executions of the target class and its subclasses to
@target() Match the class whose target object is annotated by a specific annotation

Final class structure chart

1,Factory

package com.test.aspectj.expression;

/**
 * Factory interface
 */
public interface Factory {

    // Make products
    void make();

    // transport
    void delivery(String address);
}

2,PhoneFactory

package com.test.aspectj.expression;

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

/**
 * Mobile phone factory
 */
@Component
public class PhoneFactory implements Factory {

    // How to make products
    @Override
    @Log
    public void make() {
        System.out.println("From target class PhoneFactory Message: making mobile phones");
    }

    // How to transport mobile phones
    @Override
    public void delivery(String address) {
        System.out.println("From target class PhoneFactory Message for: Transport mobile phone to " + address);
    }

    // Test @ Within annotation
    public void testWithin() {
    }
 }

3. Huawei PhoneFactory, a subclass of PhoneFactory, will be matched by @ Before("target(com.test.aspectj.expression.PhoneFactory)") defined in TargetAspect; with @ run custom annotation, it will be matched by @ After("@target(com.test.aspectj.expression.target.Run)") defined in TargetAspect

package com.test.aspectj.expression.target;

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

/**
 * Huawei mobile phone factory
 */
@Run
@Component
public class HuaweiPhoneFactory extends PhoneFactory {
}

4. Run custom annotation

package com.test.aspectj.expression.target;

import java.lang.annotation.*;

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

5. Facet class 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 And @ target test
 */
@Aspect
public class TargetAspect {

    /**
     * target() When the target type is the specified type, all methods in the target type and descendant class can match the
     *
     * This example shows that all methods of PhoneFactory and its subclass Huawei PhoneFactory can match the
     */
    @Before("target(com.test.aspectj.expression.PhoneFactory)")
    public void before() {
        System.out.println("Message from aspect: target Match to, enhance before method execution");
    }

    /**
     * @taret() Match classes annotated with the specified annotation
     *
     * HuaweiPhoneFactory With @ Run annotation, all methods of Huawei phonefactory will match to
     * PhoneFactory There is no @ Run annotation, so all PhoneFactory methods will not match
     */
    @After("@target(com.test.aspectj.expression.target.Run)")
    public void after() {
        System.out.println("Message from aspect:@target Match to, enhance after method execution");
    }
}

 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. Test code

package com.test.aspectj.expression.target;

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

/**
 * target And @ target test
 */
public class TargetExpressionDemo {
    public static void main(String[] args) {
        System.out.println("=============== test target and @target ===============");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjtargetexpression.xml");
        System.out.println("-----PhoneFactory Be reinforced-----");
        PhoneFactory phoneFactory = (PhoneFactory) context.getBean("phoneFactory");
        phoneFactory.make();
        System.out.println("-----PhoneFactory Subclasses HuaweiPhoneFactory Can also be enhanced-----");
        HuaweiPhoneFactory huaweiPhoneFactory = (HuaweiPhoneFactory) context.getBean("huaweiPhoneFactory");
        huaweiPhoneFactory.make();
    }
}

8. Operation results

===============Test target and @ target===============
-----PhoneFactory enhanced-----
Message from Facet: target matches to, enhance before method execution
Message from target class PhoneFactory: production phone
-----The subclass Huawei PhoneFactory of PhoneFactory can also be enhanced-----
Message from Facet: target matches to, enhance before method execution
Message from target class PhoneFactory: production phone
Message from Facet: @ target matches to, enhanced after method execution

Tags: Programming Mobile Spring xml Java

Posted on Thu, 30 Jan 2020 07:49:56 -0800 by teng84