AspectJ切点指示器06within和@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匹配到,执行增强

 

以上是 AspectJ切点指示器06within和@within 的全部内容, 来源链接: utcz.com/z/513086.html

回到顶部