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>
 

spring的注解AOP配置的更多相关文章

  1. Spring入门4.AOP配置深入

    Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...

  2. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  3. (spring-第4回【IoC基础篇】)spring基于注解的配置

    基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...

  4. Spring基于注解的配置概述

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...

  5. Spring基于注解@Required配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  6. Spring 基于注解的配置 简介

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  7. spring 代理注解 <aop:aspectj-autoproxy />

    spring默认使用jdk的代理方式,使用jdk的代理方式我们知道,代理的类需要实现一个接口,若果没有就会报,java.lang.NoSuchMethodException: com.sun.prox ...

  8. java Spring 基于注解的配置(一)

    注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. Spring 自定义注解,配置简单日志注解

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

随机推荐

  1. DASCTF 安恒七月赛wp

    web Ezfileinclude 首页一张图片,看src就可以看出文件包含 验证了时间戳 尝试用php://filter 读源码读不到,以为只能读.jpg,然后用../路径穿越有waf 最后居然一直 ...

  2. es date_histogram强制补零

    es补零 GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { ...

  3. 13.Fibonacci数

    描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为 F(n)=1 ...........(n=1或n=2) F(n)=F(n-1)+F(n ...

  4. Python基础(dict与set)

    #和list比较,dict有以下几个特点: #查找和插入的速度极快,不会随着key的增加而变慢: #需要占用大量的内存,内存浪费多. #dict1 = {'傻狗1':100,'傻狗2':200,'傻狗 ...

  5. QuantumTunnel:v1.0.0 正式版本发布

    经过一段时间运行,代码已经稳定是时候发布正式版本了! v1.0.0 正式版本发布 对核心能力的简要说明: 支持协议路由和端口路由:QuantumTunnel:端口路由 vs 协议路由 基于Netty实 ...

  6. Github webhooks 自动部署博客文章,使用总结【含视频】

    作者:小傅哥 博客:https://bugstack.cn 原文:https://mp.weixin.qq.com/s/VtTHUfyiITNSoGy052jkXQ 沉淀.分享.成长,让自己和他人都能 ...

  7. 『学了就忘』Linux软件包管理 — 44、在RPM包中提取文件

    目录 1.RPM包中文件的提取 2.在RPM包中提取文件的操作 (1)cpio命令介绍 (2)提取RPM包中文件 1.RPM包中文件的提取 为什么要做这个事呢? 在操作Linux系统的时候误删除一个文 ...

  8. 关于新手使用mpb开发代码的一些小知识

    mac 前端端口占用解决   1.查看8080端口,复制进程PID  lsof -i :8080 2.杀死进程 kill -9 「进程PID」   如果你的mac不能使用sudo解决办法   错误提示 ...

  9. myeclipse与tomcat,运行jsp程序

    一.myeclipse中配置JRE 步骤: 1.选择window->preferences->Java->Installed JREs 2.点击窗口右边的"add" ...

  10. Kotlin小测试

    fun main(args: Array<String>) { var a=1 a=2 println(a)//2 println(a::class)//int (Kotlin refle ...