• spring的注解AOP配置


    package com.hope.service.impl;

    import com.hope.service.IAccountService;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Service;

    /**
    * @author newcityman
    * @date 2019/11/22 - 23:27
    */
    @Service("accountService")
    public class AccountService implements IAccountService {

    public void saveAccount() {
    System.out.println("保存");
    // int i=1/0;
    }

    }

    package com.hope.utils;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;

    /**
    * @author newcityman
    * @date 2019/11/22 - 23:29
    * 记录日志的类
    */
    @Component("logger")
    @Aspect
    public class Logger {
    @Pointcut("execution(* com.hope.service.impl.*.*(..))")
    private void pt1(){};
    /**
    * 前置通知
    */
    @Before("pt1()")
    public void beforePrintLog(){
    System.out.println("前置通知");
    }

    /**
    * 后置通知
    */
    @AfterReturning("pt1()")
    public void afterReturningPrintLog(){
    System.out.println("后置通知");
    }

    /**
    * 异常通知
    */
    @AfterThrowing("pt1()")
    public void afterThrowingPrintLog(){
    System.out.println("异常通知");
    }

    /**
    * 最终通知
    */
    @After("pt1()")
    public void afterPrintLog(){
    System.out.println("最终通知");
    }

    /**
    * 环绕通知
    */
    @Around("pt1()")
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
    Object rtValue = null;
    try {
    System.out.println("前置通知");
    Object[] args = pjp.getArgs();
    rtValue= pjp.proceed(args);
    System.out.println("后置通知");
    return rtValue;
    } catch (Throwable t) {
    System.out.println("异常通知");
    throw new RuntimeException(t);
    }finally {
    System.out.println("最终通知");
    }

    }
    }
    
    
    <?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"
    xmlns:context="http://www.springframework.org/schema/context"
    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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置扫描包的路径-->
    <context:component-scan base-package="com.hope"/>
    <!--开启spring对注解AOP的支持-->
    <aop:aspectj-autoproxy/>

    </beans>
     
  • 相关阅读:
    Centos7 安装Postgres11(更改数据目录)
    将trj保存成.gpx文件方便进行地图匹配(来自徐博士的支援)
    将北京路网OSM文件导入到PostgreSQL + PostGIS 中,并利用osm2pgrouting工具+osmosis工具构建路网Graph拓扑结构
    SQL-时间-UTC-时间戳-日期-年查询在PG+PostGIS
    地理坐标系4326--投影坐标系3857/2436
    基于postgis时空查询-记录而已
    Java 接口
    单例模式
    weblogic启动一闪而过
    oracle存储过程中is和as区别
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11919513.html
Copyright © 2020-2023  润新知