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. docker容器运行java后台程序,存到数据库的时间差一天的问题

    主要原因是docker容器中的时间用的是标准时间,不是用的宿主机的时间. 修改方法: docker run -e TZ="Asia/Shanghai" -d -p 80:80 -- ...

  2. 新技能GET!在前端表格中花式使用异步函数的奥义

    背景 60年代时,操作系统中独立运行的单元通常是进程.但随着计算机技术的发展,人们发现在进程运行过程中,创建.撤销与切换都要花费较大的时空开销. 到了80年代为了解决这一问题,出现了更小的独立运行基本 ...

  3. 菜鸡的Java笔记 - java 线程常用操作方法

    线程常用操作方法        线程的命名操作,线程的休眠,线程的优先级            线程的所有操作方法几乎都在 Thread 类中定义好了            线程的命名和取得      ...

  4. 谱文混排之lilypond-book

    Lilypond有自带的谱文混排的工具lilypond-book,但是作为外行,一直很难搞清楚这个操作是怎样做的.很久之前请教过别人,但介于我的个人能力,只有粗浅理解,操作不得要领.在许多信息的拼凑之 ...

  5. IDEA远程快速部署SpringBoot项目到Docker环境

    一:基础准备 1.首先在linux服务器安装Docker环境,具体安装步骤及Docker使用参考官网或网络资料(这里重点是快速部署项目到Docker环境) 2.配置Docker远程连接端口 1.vim ...

  6. JavaScript 函数声明和变量声明

    声明语句:声明语句是用来声明或定义标识符(变量和函数名)并给其赋值. 1:var 变量声明(5.3.1节): var语句用来声明一个或多个变量:var name_1 = [= value_1] [ , ...

  7. Flink 是如何统一批流引擎的

    关注公众号:大数据技术派,回复"资料",领取1000G资料. 本文首发于我的个人博客:Flink 是如何统一批流引擎的 2015 年,Flink 的作者就写了 Apache Fli ...

  8. Atcoder Grand Contest 016 F - Games on DAG(状压 dp)

    洛谷题面传送门 & Atcoder 题面传送门 如何看待 tzc 补他一个月前做的题目的题解 首先根据 SG 定理先手必输当且仅当 \(\text{SG}(1)=\text{SG}(2)\). ...

  9. 洛谷 P4709 - 信息传递(置换+dp)

    题面传送门 一道挺有意思的题罢-- 首先看到这种与置换乘法相关的题,首先把这些置换拆成一个个置换环,假设输入的置换有 \(m\) 个置换环,大小分别为 \(s_1,s_2,\cdots,s_m\),显 ...

  10. c++STL容器之string容器

    本质:string是c++风格的字符串,而string本质上是一个类 string和char*的区别: char*是一个指针: string是一个类,类内部封装了char*,管理这个字符串,是一个ch ...