用springAOP计算每一层方法执行的时间

来源:互联网 发布:手机淘宝怎么删晒图 编辑:程序博客网 时间:2024/05/18 00:34

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>



单元测试


@Testpublic 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当前的方法名字是savePersonsavePerson300030003001



0 0