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

@annotation()概述

@annotation表示标注了某个注解的所有方法。

下面通过一个实例说明@annotation()的用法。 AnnotationTestAspect定义了一个后置切面增强,该增强将应用到标注了NeedTest的目标方法中。


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

这里写图片描述

首先我们先自定义一个注解@NeedTest。

如何自定义注解请参考Java-Java5.0注解解读

package com.xgj.aop.spring.advisor.aspectJ.function;

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: NeedTest
 * 
 * @Description: 自定义注解@NeedTest
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年8月26日 下午11:19:12
 */

// 声明注解的保留期限
@Retention(RetentionPolicy.RUNTIME)
// 声明可以使用该注解的目标类型
@Target(ElementType.METHOD)
@Documented
public @interface NeedTest {
	// 声明注解成员
	boolean value() default false;
}

下面我们定义接口 Waiter

package com.xgj.aop.spring.advisor.aspectJ.function;

public interface Waiter {
	public void greetTo(String clientName);

	public void serverTo(String clientName);
}

接口实现类 两个NaiveWaiter 和 NaughtWaiter

package com.xgj.aop.spring.advisor.aspectJ.function;

public class NaiveWaiter implements Waiter {

	@NeedTest(true)
	@Override
	public void greetTo(String clientName) {
		System.out.println("NaiveWaiter:greet to " + clientName);
	}

	@Override
	public void serverTo(String clientName) {
		System.out.println("NaiveWaiter:server to " + clientName);
	}

	public void smile(String clientName, int times) {
		System.out.println("NaiveWaiter:smile to  " + clientName + " " + times
				+ " times");
	}
}

package com.xgj.aop.spring.advisor.aspectJ.function;

public class NaughtWaiter implements Waiter {

	@Override
	public void greetTo(String clientName) {
		System.out.println("NaughtWaiter:greet to " + clientName);
	}

	@NeedTest(true)
	@Override
	public void serverTo(String clientName) {
		System.out.println("NaughtWaiter:server to " + clientName);
	}

	public void joke(String clientName, int times) {
		System.out.println("NaughtyWaiter:play " + times + " jokes to "
				+ clientName);
	}
}

我们可以看到 NaiveWaiter#greetTo()方法标注了@NeedTest, NaughtWaiter#serverTo()也标注了@NeedTest,我们的目标就是将后置增强织入到这两个标注了@NeedTest的方法中。


接下来编写切面的横切逻辑

package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

/**
 * 
 * 
 * @ClassName: AnnotationTestAspect
 * 
 * @Description: 切面 、 后置增强 ,@annotation表示标注了某个注解的所有方法
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年8月26日 下午11:23:53
 */

@Aspect
public class AnnotationTestAspect {

	@AfterReturning("@annotation(com.xgj.aop.spring.advisor.aspectJ.function.NeedTest)")
	public void needTest() {
		System.out.println("needTest() executed,some logic is here");
	}

}

接下来通过Spring自动应用切面,配置文件如下

<?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"
	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">

<!-- 使用基于Schema的aop命名空间进行配置 -->
 

<!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy/>
 
<!-- 目标Bean -->
<bean id="naiveWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaiveWaiter"/>

<bean id="naughtWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaughtWaiter"/>

<!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.annotationFun.AnnotationTestAspect"/>

</beans>

最后编写测试代码:

package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xgj.aop.spring.advisor.aspectJ.function.Waiter;

public class AnnotationTestAspcetTest {

	@Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml");

		// 必须是接口类型,否则抛类型转换异常
		Waiter waiter = (Waiter) ctx.getBean("naiveWaiter");

		// 因为greetTo标注了@NeedTest,因此会被后置增强
		waiter.greetTo("XiaoGongJiang");
		waiter.serverTo("XiaoGongJiang");

		Waiter naughtWaiter = (Waiter) ctx.getBean("naughtWaiter");
		// serverTo标注了@NeedTest,因此会被后置增强
		naughtWaiter.serverTo("XiaoGongJiang");
	}
}

运行结果:

2017-08-27 01:24:22,551  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ac604: startup date [Sun Aug 27 01:24:22 BOT 2017]; root of context hierarchy
2017-08-27 01:24:22,647  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml]
NaiveWaiter:greet to XiaoGongJiang
needTest() executed,some logic is here
NaiveWaiter:server to XiaoGongJiang
NaughtWaiter:server to XiaoGongJiang
needTest() executed,some logic is here

从输出结果中可以看出,切面被正确的织入到了标注有@NeedTest注解的方法中。

可以通过在方法前后添加时间戳,然后计算时间差来计算方法耗时。也可以使用Spring AOP的拦截器,在方法执行前后记录时间戳,然后计算时间差。以下是一个使用Spring AOP计算方法耗时的示例: 1. 定义一个切面类,实现MethodInterceptor接口 ```java @Component @Aspect public class TimingAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { long startTime = System.currentTimeMillis(); Object result = invocation.proceed(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(invocation.getMethod().getName() + " 方法耗时:" + elapsedTime + "ms"); return result; } } ``` 2. 在配置文件中启用AOP,并将切面类作为切点 ```xml <aop:aspectj-autoproxy /> <bean id="timingAspect" class="com.example.TimingAspect" /> <aop:config> <aop:aspect ref="timingAspect"> <aop:pointcut expression="execution(* com.example.service.*.*(..))" /> <aop:around method="invoke" /> </aop:aspect> </aop:config> ``` 3. 在需要计算耗时的方法上加上@LogExecutionTime注解 ```java @Service public class MyService { @LogExecutionTime public void doSomething() { // do something } } ``` 4. 定义@LogExecutionTime注解和切面类 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LogExecutionTime { } @Component @Aspect public class TimingAspect { @Around("@annotation(com.example.LogExecutionTime)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(joinPoint.getSignature().getName() + " 方法耗时:" + elapsedTime + "ms"); return result; } } ``` 这样,在调用doSomething方法时,就会输出方法耗时的信息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值