【實例】使用Spring AOP進行業務增強(通過XML配置)

  • 使用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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章