1.创建相应的类

2.代码

service沿用前面的

增加两个log

Log.java

package com.shao.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
//Method:受执行的目标对象的方法
//objects: 参数
//o:目标对象
public void before(Method method, Object[] objects, Object o) throws Throwable {
assert o != null;
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
}

AfterLog.java

package com.shao.log;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class AfterLog implements AfterReturningAdvice { //执行之后拿回了返回值
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+", 返回了"+o);
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="userService" class="com.shao.service.UserServiceImpl"/>
<bean id="log" class="com.shao.log.Log"/>
<bean id="afterLog" class="com.shao.log.AfterLog"/> <!--配置Aop:导入AOP约束-->
<aop:config>
<!--切入点:expression表达式:要执行的位置-->
<!--这个类的所有方法-->
<aop:pointcut id="pointcut" expression="execution(* com.shao.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<!--把log这个类切入到这个方法中去-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>

3.测试

import com.shao.service.UserService;
import com.shao.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
@org.junit.Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//一定要注意:动态代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

AOP实现方式一的更多相关文章

  1. Spring两种实现AOP的方式

    有两种实现AOP的方式:xml配置文件的方式和注解的形式 我们知道通知Advice是指对拦截到的方法做什么事,可以细分为 前置通知:方法执行之前执行的行为. 后置通知:方法执行之后执行的行为. 异常通 ...

  2. 来一手 AOP 注解方式进行日志记录

    系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...

  3. 做接口自动化时候,一些登录头信息可以通过aop的方式进行增强

    做接口自动化时候,一些登录头信息可以通过aop的方式进行增强

  4. Spring AOP配置方式

    AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...

  5. 在.NET Core中三种实现“可插拔”AOP编程方式(附源码)

    一看标题肯定会联想到使用动态编织的方式实现AOP编程,不过这不是作者本文讨论的重点. 本文讨论另外三种在netcore中可实现的方式,Filter(过滤器,严格意义上它算是AOP方式),Dynamic ...

  6. Spring里的aop实现方式和源码分析

    使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点.业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点.横切关注点的一个特点是,他们经常发生在核心关 ...

  7. AOP注解方式ApsectJ开发

    AOP注解方式ApsectJ开发 引入配置文件 编写切面类配置 使用注解的AOP对象目标类进行增强 在配置文件中开启以注解形式进行AOP开发 在切面类上添加注解 注解AOP通知类型 @Before前置 ...

  8. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

  9. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  10. (转)Spring AOP实现方式(转)

    我们可以通过三种方式来使用Spring AOP,它们分别是:@Aspect-based(Annotation),Schema-based(XML),以及底层的Spring AOP API 底层的Spr ...

随机推荐

  1. visualbox安装ubuntu18过程中不能输入name,password,密码,姓名问题的解决办法+一些基本操作

    ​ 问题描述:visualbox安装ubuntu18,,,,卡在注册处,无法输入姓名,密码-点击无法输入............如下图: ​ 解决办法:更改你的Ubuntu的主板内存,主板内存默认的值 ...

  2. Java基础之(五):数据类型

    Java快捷键 首先我们先来介绍下IDEA的一些快捷键 psvm=public static void main(String[] args) {} sout=System.out.println(& ...

  3. FastAPI 学习之路(七)字符串的校验

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  4. appium操作安卓应用所需要的数据准备

    操作系统.系统版本如下所示: desired_caps={} desired_caps["platformName"]="Android" desired_ca ...

  5. go-zero 实战之 blog 系统

    go-zero 实战项目:blog 本文以 blog 的网站后台为例,着重介绍一下如何使用 go-zero 开发 blog 的用户模块. 本文涉及的所有资料都已上传 github 仓库 kougazh ...

  6. 关于tkinter

    tkinter介绍 tkinter是python自带的GUI库,是对图形库TK的封装tkinter是一个跨平台的GUI库,开发的程序可以在win,linux或者mac下运行 组件概念 一个窗口中任意内 ...

  7. UltraSoft - Beta - Scrum Meeting 1

    Date: May 17th, 2020. Scrum 情况汇报 进度情况 组员 负责 今日进度 q2l PM.后端 维护Beta阶段文档 Liuzh 前端 增加删除操作按钮 Kkkk 前端 查询增加 ...

  8. 疯狂Java基础Day1

    --每过一遍基础,都是一次提升! 太多遗忘了,慢慢补... 推一个Java学习教程--->b站搜:狂神说Java系列(排序完毕) 推荐原因:讲的不错,会涉及到底层,也会讲讲面试. 一.注释 主要 ...

  9. 力扣 - 剑指 Offer 57 - II. 和为s的连续正数序列

    题目 剑指 Offer 57 - II. 和为s的连续正数序列 思路1(双指针/滑动窗口) 所谓滑动窗口,就是需要我们从一个序列中找到某些连续的子序列,我们可以使用两个for循环来遍历查找,但是未免效 ...

  10. 转:VIVADO使用技巧:设置DCI与内部参考电压

    本文转自:Vivado使用技巧(12):设置DCI与内部参考电压 - 灰信网(软件开发博客聚合) (freesion.com) DCI与内部参考电压 Xilinx FPGA提供了DCI(Digital ...