使用Spring AOP 计算API执行时间。要求使用AspectJ 切入点表达式来配置。(用xml配置)
打印出api执行的时间,要求是微秒为单位(System.currentTimeMillis),使用around。
设定一个超时时间,如果测试API超过该时间,则报错,使用finallyMethod。
增强类
package com.bamzhy.advice;
import com.bamzhy.exception.MyTimeOutException;
import com.bamzhy.utils.MyC3P0DataSouce;
import org.aspectj.lang.ProceedingJoinPoint;
public class NewMyAdvice {
double time;
//around方法
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Object proceed=null;
double start=System.currentTimeMillis();
proceed=joinPoint.proceed();
double end=System.currentTimeMillis();
time =end-start;
System.out.println("运行了这么多时间"+time);
return proceed;
}
//finallyMethod方法
public void finallyMethod(){
if (time>2000){
throw new MyTimeOutException("太慢了");
}
}
}
- XML文件(重点)
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--xml配置方法-->
<bean id="advice" class="com.bamzhy.advice.NewMyAdvice"/>
<aop:config>
<!--aspect填写注入的实例类的id-->
<aop:aspect ref="advice">
<!--pointcut填写切入点属性-->
<aop:pointcut
id="mypointcut"
expression="execution (public boolean com.bamzhy.service.UserServiceImpl.transfer(..))"/>
<!--这里填写method名和pointcut名-->
<aop:around method="around" pointcut-ref="mypointcut"/>
<aop:after method="finallyMethod" pointcut-ref="mypointcut"/>
</aop:aspect>
</aop:config>
<!--这里是加强方法所在类-->
<bean id="service" class="com.bamzhy.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--这个是别人需要实例化的对象,提供给别人ref使用-->
<bean id="userDao" class="com.bamzhy.dao.UserDaoImpl"/>
</beans>
- service层
package com.bamzhy.service;
import com.bamzhy.bean.Account;
import com.bamzhy.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
public class UserServiceImpl implements UserService {
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
private UserDao userDao;
@Override
public boolean transfer(String usernameFrom, String usernameTo, int money) throws SQLException {
Account accountFrom = userDao.getAccount(usernameFrom);
Account accountTo = userDao.getAccount(usernameTo);
accountFrom.setMoney(accountFrom.getMoney()-money);
accountTo.setMoney(accountTo.getMoney()+money);
if (!userDao.updateAccount(accountFrom))
return false;
//int i = 1/0;
if (!userDao.updateAccount(accountTo))
return false;
return true;
}
}
- dao层
package com.bamzhy.dao;
import com.bamzhy.bean.Account;
import com.bamzhy.utils.TransactionManagement;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
//Dao层
public class UserDaoImpl implements UserDao {
@Override
public boolean updateAccount(Account account) throws SQLException {
QueryRunner queryRunner=new QueryRunner();
String sql="update homework set money = ? where username = ?;";
//public int update(Connection conn, String sql, Object... params)
int update = queryRunner.update(TransactionManagement.getConnection(),sql,account.getMoney(), account.getUsername());
return update==1?true:false;
}
@Override
public Account getAccount(String username) throws SQLException {
QueryRunner queryRunner=new QueryRunner();
String sql="select * from homework where username = ?;";
Account query = queryRunner.query(TransactionManagement.getConnection(), sql, new BeanHandler<Account>(Account.class), username);
return query;
}
}