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

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



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值