spring的aop配置

来源:互联网 发布:js pagehide 编辑:程序博客网 时间:2024/05/18 21:08

beans.xml配置文件

<?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"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd           http://www.springframework.org/schema/mvc          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx.xsd"       default-autowire="byName">    <!-- 启用spring mvc 注解 -->    <context:annotation-config/>    <mvc:annotation-driven/>    <!--添加aop代理的支持-->    <aop:config proxy-target-class="true"/>    <aop:aspectj-autoproxy proxy-target-class="true"/>
package com.xxxx.util;import org.apache.log4j.Logger;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;import java.util.Date;/** * 计算时间的切面 * Created by yangshusheng on 2016/9/22. */@Aspect@Componentpublic class TimeAspect {    private Logger logger = Logger.getLogger(TimeAspect.class);    //com.xxxx下面的所有包的所有方法,所有参数或所有的返回值    //表示切面的范围是com.xxxx下面的任何包下面的任何类的任何方法,可以任何参数    @Pointcut("execution(* com.xxx.*.*.*(..))")    private void pointCutMethod() {    }    /**     * 方法执行前将执行的方法     *///    @Before("pointCutMethod()")    public void doBefore() {        System.out.println("前置通知");        logger.info("前置通知");    }    /**     * 方法执行结束返回结果后执行的方法,     *     * @param result 返回值     *///    @AfterReturning(pointcut = "pointCutMethod()", returning = "result")    public void doAfterReturning(String result) {        logger.info("后置通知");        logger.info("result: " + result + "---");    }    /**     * 声明例外通知     * @param e 抛出的错误     *///    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")    public void doAfterThrowing(Exception e) {        logger.info("例外通知");        logger.info(e.getMessage());    }    /**     * 声明最终通知     *///    @After("pointCutMethod()")    public void doAfter() {        logger.info("最终通知");    }    /**     * 声明环绕通知     * @param pjp     * @return     * @throws Throwable     */    @Around("pointCutMethod()")    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {        logger.info("进入方法---环绕通知");        Date startTime = new Date();        logger.info("方法开始执行时间" + startTime + ".");        Object o = pjp.proceed();        logger.info("退出方法---环绕通知");        Date endTime = new Date();        logger.info("方法结束执行时间" + endTime + ",总执行时间: " + (endTime.getTime() - startTime.getTime()) + "毫秒");        return o;    }}
1 0
原创粉丝点击