Spring aop 简单示例

简单的记录一下spring aop的一个示例

基于两种配置方式:

      基于xml配置

      基于注解配置

这个例子是模拟对数据库的更改操作添加事物

其实并没有添加,只是简单的输出了一下记录

首先看下整个例子的目录图

  

全部代码就不贴了,数目有点多,不过很简单,看一部分就能够明白

第一种配置方式

  基于xml方式配置

  首先将service,dao注册到spring容器

  

  配置一下扫描包还是很方便的

  接下来看下service

  

 1 package com.yangxin.core.service.impl;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Service;
5
6 import com.yangxin.core.dao.UserDao;
7 import com.yangxin.core.pojo.User;
8 import com.yangxin.core.service.UserService;
9
10 @Service
11 public class UserServiceImpl implements UserService {
12
13 @Autowired
14 private UserDao userDao;
15
16 @Override
17 public void addUser(User user) {
18 userDao.insertUser(user);
19 System.out.println("添加成功");
20 }
21
22 @Override
23 public void deleteUser(String name) {
24 userDao.deteleUser(name);
25 System.out.println("删除成功");
26 }
27
28 }

要做的事情很简单,插入一条数据,删除一条数据

接下来看下切面代码

 1 package com.yangxin.core.transaction;
2
3 import org.aspectj.lang.ProceedingJoinPoint;
4
5 import com.yangxin.core.pojo.User;
6
7 public class TransactionDemo {
8
9 //前置通知
10 public void startTransaction(){
11 System.out.println("begin transaction ");
12 }
13
14 //后置通知
15 public void commitTransaction(){
16 System.out.println("commit transaction ");
17 }
18
19 //环绕通知
20 public void around(ProceedingJoinPoint joinPoint) throws Throwable{
21 System.out.println("begin transaction");
22
23 joinPoint.proceed();
24
25 System.out.println("commit transaction");
26 }
27
28 }

然后看下这个切面在applicationContext.xml中是如何配置的

 

 1 <aop:config>
2 <aop:pointcut expression="execution(* com.yangxin.core.service.*.*.*(..))" id="p1" />
3
4 <aop:aspect ref = "transactionDemo">
5
6 <aop:before method="startTransaction" pointcut-ref="p1" />
7
8 <aop:after-returning method="commitTransaction" pointcut-ref="p1"/>
9
10 </aop:aspect>
11 </aop:config>

这里没有演示环绕通知

好了,运行测试代码

测试代码如下

 1   @Test
2 public void test1(){
3 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
4
5 UserService userService = applicationContext.getBean(UserService.class);
6
7 User user = new User();
8
9 user.setAge(19);
10 user.setName("yangxin");
11
12 userService.addUser(user);
13 userService.deteleUser("yangxin");
1415
16 }

控制台输出如下

  begin transaction

  添加成功

  commit transaction

  begin transaction

  删除成功

  commit transaction

现在来测试一下环绕通知

修改一下applicationContext.xml中的配置切面那一部分

修改后的代码

1 <aop:config>
2 <aop:pointcut expression="execution(* com.yangxin.core.service.*.*.*(..))" id="p1" />
3
4 <aop:aspect ref = "transactionDemo">
5
6 <aop:around method="around" pointcut-ref="p1"/>
7
8 </aop:aspect>
9 </aop:config>

运行测试代码

输出如下

begin transaction
添加成功
commit transaction
begin transaction
删除成功
commit transaction

好了,现在贴下如何用注解的方法

贴下基于注解的切面的代码

 1 package com.yangxin.core.transaction;
2
3 import org.aspectj.lang.ProceedingJoinPoint;
4 import org.aspectj.lang.annotation.AfterReturning;
5 import org.aspectj.lang.annotation.Around;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Before;
8 import org.aspectj.lang.annotation.Pointcut;
9
10 @Aspect
11 public class TransactionDemo2 {
12
13 @Pointcut(value="execution(* com.yangxin.core.service.*.*.*(..))")
14 public void point(){
15
16 }
17
18 @Before(value="point()")
19 public void before(){
20 System.out.println("transaction begin");
21 }
22
23 @AfterReturning(value = "point()")
24 public void after(){
25 System.out.println("transaction commit");
26 }
27
28 @Around("point()")
29 public void around(ProceedingJoinPoint joinPoint) throws Throwable{
30 System.out.println("transaction begin");
31 joinPoint.proceed();
32 System.out.println("transaction commit");
33
34 }
35 }

在applicationContext.xml中配置

1 <bean id = "transactionDemo2" class = "com.yangxin.core.transaction.TransactionDemo2" />
1 <aop:aspectj-autoproxy />

测试步骤和以上一致,这里就不贴了

完毕

记一下使用javaConfig配置方式的一些坑

以上 切面 不管是基于注解的还是基于xml配置的   这里把切面加载到容器中都是用xml配置bean的方式

如果用javaConfig方式的话

需要在配置类上加上 @EnableAspectJAutoProxy 注解

然后如果使用扫包的方式配置切面bean的话

切面上除了要加上@Aspect注解标识为这是一个切面bean之外

还需要在上面加上@component这个注解

很急很关键

排错的时候只关注切点表达式的问题了

当时一直在找切点表达式的问题

其实这个表达式写的是没有问题的

是配置切面bean的时候出的问题

深坑

aop的简单使用(代码和配置记录)的更多相关文章

  1. java:Spring框架1(基本配置,简单基础代码模拟实现,spring注入(DI))

    1.基本配置: 步骤一:新建项目并添加spring依赖的jar文件和commons-logging.xx.jar: 步骤二:编写实体类,DAO及其实现类,Service及其实现类; 步骤三:在src下 ...

  2. 云笔记项目-AOP知识简单学习

    在云笔记项目的过程中,需要检查各个业务层的执行快慢,如登录.注册.展示笔记本列表,展示笔记列表等,如果在每个业务层方法里都写一段代码用来检查时间并打印,不仅仅显得代码重复,而且当项目很大的时候,将大大 ...

  3. springboot aop 自定义注解方式实现完善日志记录(完整源码)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...

  4. AngularJS-系统代码的配置和翻译

    前言:在Web开发中常常会遇到这样的情况,有些页面的下拉选项是固定不变的几个,比如:性别,一般有男.女.保密等.对于这样的情形我们一般在数据库中存储的是数字或者其对应的代码,如果是可维护的需要系统给出 ...

  5. AOP的简单练习

    ---恢复内容开始--- 1.AOP的主要作用及概念简介 AOP最大的用处在于事务处理上,业务层在项目中主要负责以下的操作: ·调用数据层进行处理: ·进行事务的处理: ·关闭数据库的连接操作: 但在 ...

  6. C# 中使用面向切面编程(AOP)中实践代码整洁

    1. 前言 最近在看<架构整洁之道>一书,书中反复提到了面向对象编程的 SOLID 原则(在作者的前一本书<代码整洁之道>也是被大力阐释),而面向切面编程(Aop)作为面向对象 ...

  7. php安全配置记录和常见错误梳理

    通常部署完php环境后会进行一些安全设置,除了熟悉各种php漏洞外,还可以通过配置php.ini来加固PHP的运行环境,PHP官方也曾经多次修改php.ini的默认设置.下面对php.ini中一些安全 ...

  8. C# 中使用面向切面编程(AOP)中实践代码整洁(转)

    出处:https://www.cnblogs.com/chenug/p/9848852.html 1. 前言 最近在看<架构整洁之道>一书,书中反复提到了面向对象编程的 SOLID 原则( ...

  9. MAC OS X Yosemite的PyQt4配置记录

    MAC OS X Yosemite的PyQt4配置记录 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途. 2)若本文档内有侵权文字或图片等内容,请联系 ...

随机推荐

  1. 云上的芯脏病:奇怪的阿里云 RDS 数据库突发 CPU 近 100% 问题

    最近遇到了奇怪的阿里云 RDS 数据库突发 CPU 近 100% 问题,遇到了3次. 第一次是10月12日(周六)凌晨 3:24 负载极低的时候开始出现,早上发现后进行了主备切换,恢复了正常. 第二次 ...

  2. Django中CKEditor富文本编译器的使用

    CKEditor富文本编辑器 1. 安装 pip install django-ckeditor 2. 添加应用 在INSTALLED_APPS中添加 INSTALLED_APPS = [ ... ' ...

  3. java猜数游戏

    java随机数的产生 int number=(int)(Math.random()*10+1) Math.random()*n //n个随机数,从0开始 do{}while循环 //猜数,1到10的随 ...

  4. 关于CSS Grid Layout的代码解释

    .wrapper { display: grid; /*生成grid类型块级网格*/ grid-template-columns: repeat(3, 1fr); /*设置显示的列网格线,且重复3次1 ...

  5. 利用Echarts实现全国各个省份数据占比,图形为中国地图

    最近项目需求,需要一个对于全国各个省份的数据分析,图形最好是地图的样子,这样子更为直观. 最先想到的图表插件是Echarts,他的文档相对于阿里的G2,G6更加清晰一些.在Echarts 里找到的个 ...

  6. Hadoop和YARN :map+shuffle+reduce走读

    今天做了一个hadoop分享,总结下来,包括mapreduce,及shuffle深度讲解,还有YARN框架的详细说明等. v\:* {behavior:url(#default#VML);} o\:* ...

  7. Ubuntu 重装vmtool

    1. 虚拟机菜单 ->  更新虚拟机  : 2. 弹出的窗口中: 3. 拷贝红色的文件到可读写的目录: 4. 解压,运行解压出来的绿色脚本文件,一路回车:

  8. Spring Boot2 系列教程(十五)定义系统启动任务的两种方式

    在 Servlet/Jsp 项目中,如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web基础中的三大组件( ...

  9. linux下mqtt-client

    CPATH += ../embe_mqtt/MQTTClient/srcPSRTPATH = ../embe_mqtt/MQTTPacket/src LOADPATH += -I$(CPATH)LOA ...

  10. pytest中unicode编码问题(如test_fix.py::Test1::test_s1[\u6d4b\u8bd5-\u6d4b\u8bd5])

    现象: 采用如下方式可将其正确显示为中文 ss = r"test_fix.py::Test1::test_s1[\u6d4b\u8bd5-\u6d4b\u8bd5]" print( ...