1、导包
2、编写切面类,在切面类上直接使用AOP注解
packagestar.july.e_spring_aop_annotation;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.aspectj.lang.annotation.After;
importorg.aspectj.lang.annotation.Around;
importorg.aspectj.lang.annotation.Aspect;
importorg.aspectj.lang.annotation.Before;
//使用注解AOP
@Aspect//代表一个切面类
publicclassMyAspect {
/**
* 常用的通知类:
* 前置通知: 在目标对象的连接点之前调用
* 后置通知:在目标对象的连接点之后调用
* 环绕通知: 在目标对象的连接点之前与之后调用
*/
@Before(value = "execution(* star.july.*.UserDao.*())")
publicvoidbefore(){
System.out.println("开启事务");
}
//定义后置通知
@After(value = "execution(* star.july.*.UserDao.update())")
publicvoidafter(){
System.out.println("结束事务");
}
//环绕通知
@Around(value = "execution(* star.july.*.UserDao.*())")
publicvoidround(ProceedingJoinPoint jp)throwsThrowable{
System.out.println("环绕通知前面代码");
//执行目标对象的核心方法
jp.proceed();//执行连接点方法
System.out.println("环绕通知后面代码");
}
}
3、编写dao类
packagestar.july.e_spring_aop_annotation;
publicclassUserDao{
//调用业务方法
publicvoidsave(){
System.out.println("调用save方法");
}
publicvoidupdate(){
System.out.println("调用update方法");
}
}
4、配置
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!-- 1.创建目标对象 -->
<beanid="userDao"class="star.july.e_spring_aop_annotation.UserDao"></bean>
<!-- 2.创建切面类对象 -->
<beanid="myaspect"class="star.july.e_spring_aop_annotation.MyAspect"></bean>
<!-- 注意:注解方式必须开启自动创建代理类配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
5、测试
package star.july.e_spring_aop_annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
UserDao userDao = (UserDao)ac.getBean("userDao");
userDao.save();
userDao.update();
}
}
可以参考上一篇文章,下方有链接
Spring 注解AOP相关推荐
- icop java,java基于spring注解AOP的异常处理的方法
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- Spring 注解AOP 入门
XML <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...
- Spring注解开发学习笔记
1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...
- spring的AOP配置之@注解方式
AOP配置(注解) spring的AOP概念在spring的AOP配置之XML方式这骗博客中详细介绍了,这篇博客就直接开始介绍AOP配置需要那些注解 AOP注解详解 @Aspect 标志为一个切面类 ...
- spring的aop注解配置(了解)
这个演示是属于XML配置的,咱们Spring AOP还有一种,注解配置,注解这个东西就是代理配置文件的,那接着来看一下,使用注解,怎么来做,那其实步骤还是一样,包还是刚才的4+2+2,准备目标对象也还 ...
- Spring之AOP系列--将方法上的注解做为切点(用@Around)
原文网址:Spring之AOP系列--将方法上的注解做为切点(用@Around)_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍Spring(SpringBoot)的AOP的用 ...
- spring注解驱动开发-5 Spring AOP实现
Spring AOP实现 前言 AOP案例实现 1.编写目标类 2.编写切面类 3.编写配置类 4.编写测试类 end... 前言 AOP为Aspect Oriented Programming的缩写 ...
- spring注解驱动开发-6 Spring AOP实现原理
Spring AOP实现原理 前言 1.@EnableAspectJAutoProxy注解原理 2.AnnotationAwareAspectJAutoProxyCreator 分析 1.分析前工作, ...
最新文章
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介...
- 动态控制jQuery easyui datagrid工具栏显示隐藏
- 关于如何升级gcc版本及相关的标准库问题
- (转)为什么人生气时说话用喊的?
- 终极JPA查询和技巧列表–第2部分
- Sum of AP series——AP系列之和
- STM32:关于DMA,TCM(ITCM和DTCM)和Cache的理解
- Redis之-字符串类型
- 【时间序列分析】01. 时间序列·平稳序列
- windows共享 无法访问
- 访问路由出现An error occurred
- 微信公众平台之CURL应用access_token
- 大数据24小时:孙彬出任乐视云新CEO,趣店数百万学生信息数据疑似泄露
- 第三方支付系统-支付流程
- 一张图掌握10个表格和工具让你轻松做好项目管理
- java读取nas文件_Windows store 8 app - 从NAS驱动器播放文件
- vb.net画图程序介绍
- pyplot绘制图片_使用matplotlib的pyplot模块绘图的实现示例
- 物业生活缴费小程序开发制作
- 简单易懂的小游戏(不来试试吗?)
热门文章
- Html 教程 (7)布局
- 跨链(5)“蚂蚁区块链”之跨链数据连接服务
- python字节码大全
- [密码学] 基础知识
- 05-CA/TA编程:hmac demo
- android/linux中的第一个init程序的启动
- SEO -- 搜索引擎优化
- 2020-12-3(ESP定律脱壳理解)
- *ctf 逆向math题解
- 1.9 Java转换流:InputStreamReader和OutputStreamWriter