写程序的时候也许同样的功能有不同的实现方法,这时候方法的运行时间能很大程度上衡量一种实现方式的好坏,好了,获取方法的运行时间就是在方法执行的前后获取时间做减,结果就出来了,这正是aop的around的功能,然后考虑到一种情况,如果需要了解很多方法的执行时间,还会对每一个方法进行around操作吗,一种常用的解决方法就是在方法上打注解,然后用aop 去around注解。
实现方法如下:
1. 对spring进行配置(启动注解扫描和aop)
<span style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="com.yx.timeExector.timeExector"/>
<aop:aspectj-autoproxy/>
</beans></span>
2.自定义注解:
<span style="font-size:18px;">import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RunTime {
}</span>
RunTime注解,注解里面什么都没有仅起一种标识作用。
3.在需要测试的方法上打注解
<span style="font-size:18px;">import org.springframework.stereotype.Component;
@Component
public class TestClass {
@RunTime
public void func()
{
System.out.println("this is func");
}
}</span>
4.用aop去扫注解
<span style="font-size:18px;">@Aspect
@Component
public class TimeIntercepter {
@Around("@annotation(rt)")
public void calculateRunTime(ProceedingJoinPoint jp, RunTime rt) throws Throwable
{
//计算方法运行时间
Long time1 = System.currentTimeMillis();
jp.proceed();
Long time2 = System.currentTimeMillis();
Long runTime = time2 - time1;
//获取类名
Class<?> cls = jp.getTarget().getClass();
String clsName = cls.getName();
//获取方法签名
Signature signature = jp.getSignature();
String methodName = signature.getName();
String entireMethodName = clsName+"."+methodName+"方法";
System.out.println(entireMethodName+"运行时间为:"+runTime+" millseconds");
}
}</span>
5.测试方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class MainCls
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestClass tcls = (TestClass)applicationContext.getBean("testClass");
tcls.func();
}
}
最后给出事例所需maven依赖
<span style="font-size:18px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yx.timeExector</groupId>
<artifactId>timeExector</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>timeExector</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
</span>
总结:用aop去扫描自定义注解是一种很常用的方式去实现权限认证和日志以及事务管理,这是aop的应用场景不过配合扫注解更为实用和方便