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>
 

最新文章

  1. Hammer.js分析(三)——input.js
  2. Linux字符界面安装VMware tools
  3. js_多个引号的用法
  4. linux 学习6 软件包管理 资料链接
  5. Loadrunner中参数化实战(7)-Unique+Each iteration
  6. byte[]和InputStream的相互转换[转载]
  7. maven install 报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project*****
  8. char const*, char*const, const char *const的区别
  9. C++程序的编写和实现
  10. SonarQube代码质量管理平台安装与使用--转载
  11. Effective Java 学习笔记之第七条——避免使用终结(finalizer)方法
  12. Java提高学习之Object(4)
  13. react配置之浅谈
  14. Twitter数据非API采集方法
  15. 【JAVAWEB学习笔记】28_jqueryAjax:json数据结构、jquery的ajax操作和表单校验插件
  16. Beta Scrum Day 3
  17. mysql命令行大全
  18. python2.7升级3.5教程 可用
  19. python-基础数据类型,集合及深浅copy
  20. @Pointcut的用法

热门文章

  1. 一次Java线程池误用(newFixedThreadPool)引发的线上血案和总结
  2. 大一C语言学习笔记(8)---指针篇--动态内存是什么?与静态内存有什么区别?怎么使用动态内存,有什么需要注意的地方?
  3. Python如何格式化输出
  4. Jetpack架构组件学习(1)——LifeCycle的使用
  5. 解决异常:“The last packet sent successfully to the server was 0 milliseconds ago. ”的办法
  6. [bzoj5343]混合果汁
  7. littlevgl架构浅析
  8. LRU缓存
  9. jvm的小练习
  10. LOJ 2555 &amp; 洛谷 P4602 [CTSC2018]混合果汁(二分+主席树)