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>
spring的注解AOP配置的更多相关文章
- Spring入门4.AOP配置深入
Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
- Spring基于注解的配置概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...
- Spring基于注解@Required配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring 基于注解的配置 简介
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- spring 代理注解 <aop:aspectj-autoproxy />
spring默认使用jdk的代理方式,使用jdk的代理方式我们知道,代理的类需要实现一个接口,若果没有就会报,java.lang.NoSuchMethodException: com.sun.prox ...
- java Spring 基于注解的配置(一)
注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Spring 自定义注解,配置简单日志注解
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
随机推荐
- nohup、&、 2>&1详解
前言 对一个程序员来说,java项目的打包部署也是一项必须掌握的一项技术任务,现我将自己平时在maven下打包以及部署项目总结,希望对有这方面诉求的小伙伴有所帮助! 一.maven项目打包及命令 (1 ...
- Hi3516开发笔记(一):海思HI3516DV300芯片介绍,入手开发板以及Demo测试
前言 目前主流国产芯片为RV11XX.RK33XX.Hi35XX系列,本系列开启Hi3516系列的开发教程. Hi3516DV300芯片介绍 Hi3516DV300为专业行Smart IP ...
- 动图图解!怎么让goroutine跑一半就退出?
光看标题,大家可能不太理解我说的是啥. 我们平时创建一个协程,跑一段逻辑,代码大概长这样. package main import ( "fmt" "time" ...
- ASP.NET Core 学习笔记 第五篇 ASP.NET Core 中的选项
前言 还记得上一篇文章中所说的配置吗?本篇文章算是上一篇的延续吧.在 .NET Core 中读取配置文件大多数会为配置选项绑定一个POCO(Plain Old CLR Object)对象,并通过依赖注 ...
- 论文解读(LLE)《Nonlinear Dimensionality Reduction by Locally Linear Embedding》and LLE
论文题目:<Nonlinear Dimensionality Reduction by Locally Linear Embedding > 发表时间:Science 2000 论文地址 ...
- [cf1392I]Kevin and Grid
令$v$为点数(有公共点的格子中存在红/蓝色).$e$为边数(有公共边的格子中存在红/蓝色).$f$为以此法(即仅考虑这些点和边)所分割出的区域数(包括外面).$s$为连通块个数,将欧拉定理简单扩展, ...
- Windows系统及硬件信息读取
Windows桌面端开发常常会需要读取系统信息或硬件信息作为用户标识,比如用于确认该设配是否已经激活程序.也可以使用随机生成的UUID来作为唯一标识,但是如果重装系统或重装软件都有可能导致标识丢失,因 ...
- Map、HashMap、Properties、TreeMap
1.掌握Map接口中常用方法. 2.遍历Map集合的两种方式都要精通. 第一种:获取所有key,遍历每个key,通过key获取value. 第二种:获取Set<Map.Entry>即可,遍 ...
- 还有这种好事!netty自带http2的编码解码器framecodec
目录 简介 Http2FrameCodec Http2Frame.Http2FrameStream和Http2StreamFrame Http2FrameCodec的构造 Stream的生命周期 流控 ...
- [SQL]master..sysprocesses
--https://docs.microsoft.com/zh-cn/sql/relational-databases/system-compatibility-views/sys-sysproces ...