用springAOP计算每一层方法调用的时间
写一个三层架构出来
Dao层
public interface PersonDao {
void savePerson();
}
public class PersonDaoImpl implements PersonDao {
public void savePerson(){
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("savePerson");
}
}
Service层
public interface PersonService {
void savePerson();
}
public class PersonSericeImpl implements PersonService {
private PersonDao personDao;//这里需要注入,用set方式
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void savePerson() {
personDao.savePerson();
}
}
Action层
public class PersonAction {
private PersonService personService;//这里也需要set方式的参数注入
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public void savePerson(){
this.personService.savePerson();
}
}
写一个切面,和环绕通知,计算每一层调目标方法的时间
/**
* 这是一个切面:
* 计算每个类中每个方法的执行时间
*
*/
public class TimeAspect {
/**
* 这是一个环绕通知
*/
public void methodExceptionTime(ProceedingJoinPoint joinPoint) throws Throwable{
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
System.out.println("当前的类是"+ className);
System.out.println("当前的方法名字是" + methodName);
Long time1 = System.currentTimeMillis();//系统的当前时间
joinPoint.proceed();//执行目标方法
System.currentTimeMillis();
Long time2 = System.currentTimeMillis();//系统的当前时间
Long time3 = time2 - time1;
System.out.println(time3);
}
}
ApplicationContext.xml 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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="personDao" class="com.mo.dao.PersonDaoImpl"></bean>
<bean id="personService" class="com.mo.service.PersonSericeImpl">
<property name="personDao" ref="personDao"></property>
</bean>
<bean id="personAction" class="com.mo.action.PersonAction">
<property name="personService" ref="personService"></property>
</bean>
<!-- 这是一个切面 -->
<bean id="timeAspect" class="com.mo.timeaspect.TimeAspect"></bean>
<!-- 配置springAOP -->
<aop:config>
<!--
配置切入点 目标类
用这个切入点表达式会将切面 也包含进去 但当 timeAspect配置为切面是 spring会自动忽略这个切面是目标类
-->
<aop:pointcut expression="execution(* com.mo..*.*(..) )"
id="perform"/>
<!-- 定义一个切面 -->
<aop:aspect ref="timeAspect">
<!-- 这是一个环绕通知 -->
<aop:around method="methodExceptionTime" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>
</beans>
单元测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonAction personAction = (PersonAction) context.getBean("personAction");
personAction.savePerson();
}
测试结果
当前的类是com.mo.action.PersonAction
当前的方法名字是savePerson
当前的类是com.mo.service.PersonSericeImpl
当前的方法名字是savePerson
当前的类是com.mo.dao.PersonDaoImpl
当前的方法名字是savePerson
savePerson
3000
3000
3001