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(面向切面编程)相关概念. ...
随机推荐
- Java学习(二十一)
今天学的访问控制权限修饰符: 和c++比多了一个缺省,基本和c++功能一样,private多了一个同包的限制. 缺省的话是同包就可访问. 基本都学过,所以理解的也很快. 然后又把最近学得总结了一下: ...
- 基于Mui与H5+开发webapp的Android原生工程打包步骤(使用新版本5+SDK与Android studio)(部分内容转自dcloud官网)
文章背景: dcloud官网给出的打包步骤对于有一定安卓打包基础的同学来说比较容易掌握,但是对于webapp小白来讲有的地方可能没有说的太具体.下面我给大家介绍的详细一点,保证大家按照步骤就能学会打包 ...
- Django 小实例S1 简易学生选课管理系统 4 实现登录页面
Django 小实例S1 简易学生选课管理系统 第4节--实现登录页面 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 本文涉及到的新的额外知识点: ...
- 菜鸡的Java笔记 - java 访问控制权限
java中四种访问控制权限的使用 内容 在java里面一共定义有四个权限,按照由小到大的顺序:private<defaule<prote ...
- 使用PAM模块实现普通用户之间su免密切换
参考自:Allow user1 to "su - user2" without password https://unix.stackexchange.com/questions/ ...
- 使用VSCode编写,发布cnblogs
WriteCnBlog插件作者写的教程: https://www.cnblogs.com/caipeiyu/p/11774968.html
- [uoj76]懒癌
为了方便,称患有懒癌的狗为"坏狗" 记$Q_{i}$为第$i$个人能观察的狗集合,$S$为坏狗集合,那么第$k$天第$i$个人能得到的信息有且仅有$S\ne \empty$.$S\ ...
- [atARC099E]Independence
考虑这张图的反图,相当于这两个集合内部没有边,这也就是二分图的限制 换言之,我们要将这张图黑白染色(不能则为-1),$x$即为某种颜色的数个数 对于一个联通块,记连通块大小为$sz$,则白色点个数为$ ...
- Python学习手册(第四版)——使用入门(自学用)
Python的优点 -可读性 -可移植性 -不是一个独立的工具,可以调用各种库,同时也可以被调用等等 -使编程变得很有趣 -面向对象 -可混合 Python的理念 随便找的一张图 脚本语言? Pyth ...
- git添加新工程
git init git remote add origin 码云路径 git pull origin master 代码拉本地后 git add . git commit -m '新添加的文件内容描 ...