只是为了自己更容易记住,理解相关内容,所以写了一篇简单的文章,主要还是参考前辈的大作完成。
因为这个案例很简单就能运行,所以不废话,主要的代码如下:
1. 先创建一个maven 的不同项目,其中配置文件主要依赖的包内容如下:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
创建完成后,使用 maven update 下载jar包,以便后续引用。
2. 创建一个对外接口UserService:
package com.tanjie.test.service;
public interface UserService {
void userSayHello();
}
3. 创建一个对外接口的实例 UserServiceImpl:
package com.tanjie.test.service;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public void userSayHello() {
// 为了更方便的显示程序运行的模式,所以增加了一个暂停1秒的线程
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("User Say Hello!");
}
}
4. 创建一个main 测试方法:
package com.tanjie.test.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tanjie.test.service.UserService;;
public class MainTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean(UserService.class);
userService.userSayHello();
context.close();
}
}
5. 在src/main/resources目录下增加Spring的配置文件applicationContext.xml,主要内容如下:
<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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="com.tanjie.test"></context:component-scan>
</beans>
至此,一个普通了spring测试的案例以及建立完成,但是还没有加入Spring的Aop
6. 增加时间监控类TimeMonitor:
package com.tanjie.test.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
@Service
@Aspect
public class TimeMonitor {
@Around("execution(* com.tanjie.test.service.UserServiceImpl.userSayHello(..))")
public void monitorAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method start time:" + System.currentTimeMillis());
Object re = pjp.proceed();
System.out.println("method end time:" + System.currentTimeMillis());
}
}
7. 运行main方法,即可看到结果。
这个是最简单的Spring Aop的案例,只有环绕方法,写一篇介绍Aop 更多的方法。
完成此项目主要参考了这篇文章,非常感谢作者,有不明白的地方,大家也可以看作者的解释:
http://www.jianshu.com/p/9517c90db0d4