1、maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ly.spring</groupId>
<artifactId>spring05</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--用于解析切入点表达式-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
<!--用于整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> </dependencies>
<!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2、实体类

package com.ly.spring.domain;

import java.io.Serializable;

public class Account implements Serializable {
private String accountName; public String getAccountName() {
return accountName;
} public void setAccountName(String accountName) {
this.accountName = accountName;
} @Override
public String toString() {
return "Account{" +
"accountName='" + accountName + '\'' +
'}';
}
}

3、service接口

package com.ly.spring.service;

import com.ly.spring.domain.Account;

import java.util.List;

public interface IAccountService {
public List<Account> findAll();
}

4、service实现类

package com.ly.spring.service.impl;

import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.springframework.stereotype.Service; import java.util.ArrayList;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Override
public List<Account> findAll() {
System.out.println("AccountServiceImpl---findAll");
List<Account> list = new ArrayList<>();
Account account = new Account();
account.setAccountName("hehe2");
list.add(account);
return list;
}
}

5、通知类

package com.ly.spring.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component; @Component("logUtil")
public class LogUtil {
public void beforeFunc() {
System.out.println("---前置通知---");
}
public void afterReturnFunc() {
System.out.println("---后置通知---");
}
public void afterThrowFunc() {
System.out.println("---异常通知---");
}
public void afterFunc() {
System.out.println("--最终通知--");
} /**
*1、spring要求给环绕通知的方法增加ProceedingJoinPoint类型的参数,可实现环绕通知的相关逻辑
*2、环绕通知方法的返回值即业务方法的返回值
*3、环绕通知方法内可具体指定前置通知、后置通知、异常通知、最终通知代码的位置
*/
public Object arroundFunc(ProceedingJoinPoint pjp) {
try {
Object result = null;
Object[] args = pjp.getArgs();//获得调用业务方法的参数
beforeFunc();//前置通知
result = pjp.proceed(args);//执行业务参数
afterReturnFunc();//后置通知
return result;
}catch (Throwable t) {
afterThrowFunc();//异常通知
throw new RuntimeException(t);
}finally {
afterFunc();//最终通知
}
}
}

6、spring配置文件

<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置注解扫描的包-->
<context:component-scan base-package="com.ly.spring"></context:component-scan>
<!--aop相关配置-->
<aop:config>
<!--id指定通知的id,ref指定通知bean-->
<aop:aspect id="logAdvisor" ref="logUtil">
<!--环绕通知-->
<aop:around method="arroundFunc" pointcut-ref="logPointCut"></aop:around>
<!--配置切入点表达式-->
<!--
1、若配置在aop:aspect标签内则只对当前切面有效
2、可以配置在aop:aspect标签外,此时aop:pointcut标签必须配置在所有的aop:aspect标签前面,对所有的切面有效
-->
<aop:pointcut id="logPointCut" expression="execution(* com.ly.spring.service.impl.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>

7、测试类

package com.ly.spring.test;

import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; //替换junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置文件的位置
@ContextConfiguration(locations = "classpath:bean.xml")
public class MainTest {
@Autowired
private IAccountService accountService;
@Test
public void test1() {
List<Account> result = accountService.findAll();
System.out.println(result);
}
}

环绕通知(xml)的更多相关文章

  1. 通知类型 重点: 环绕通知 (XML配置)

    前置通知:在切入点方法执行之前执行 <aop:before method="" pointcut-ref="" ></aop:before&g ...

  2. spring 切面 前置后置通知 环绕通知demo

    环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  3. spring框架应用系列四:切面编程(环绕通知与前后置通知区别)

    切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...

  4. 基于XML的AOP配置(2)-环绕通知

    配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*( ...

  5. spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

  6. 14Spring_AOP编程(AspectJ)_环绕通知

    在目标方法执行前后,进行代码增强 (阻止目标方法的执行 ) 环绕通知实现任何通知效果. 案例如下: 案例结构:

  7. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  8. [转载] spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

  9. aop编程之后置通知,环绕通知和异常通知

    ---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html  具体流程如下: 1. ...

随机推荐

  1. RC振荡电路

    RC振荡电路,由电阻R和电容C构成的适用于产生低频信号的电路 1.原理---简介 RC振荡电路,采用RC选频网络构成,适用于低频振荡,一般用于产生1Hz~1MHz(fo=1/2πRC)的低频信号.对于 ...

  2. WeChall_Training: ASCII (Training, Encoding)

    In a computer, you can only work with numbers.In this challenge you have to decode the following mes ...

  3. ARTS Week 14

    Jan 27, 2020 ~ Feb 2, 2020 Algorithm Problem 160.Intersection of Two Linked Lists(相交链表) 题目链接 题目描述:给定 ...

  4. 纯JavaScript实现页面行为的录制

    在网上有个开源的rrweb项目,该项目采用TypeScript编写(不了解该语言的可参考之前的<TypeScript躬行记>),分为三大部分:rrweb-snapshot.rrweb和rr ...

  5. 【译文连载】 理解Istio服务网格(第二章 安装)

    全书目录 第一章 概述 本文目录 1.命令行工具安装 2. Kubernetes/OpenShift安装 3. Istio安装 4.示例Java微服务安装 4.1 源码概览 4.2 编译和部署cust ...

  6. Linux学习1-云服务器上搭建禅道项目管理工具

    前言 相信各位测试的小伙伴出去面试总会被问到:测试环境怎么搭建?一个中级测试工程师还是对测试环境一无所知的话,面试官会一脸鄙视的,今天我给大家介绍一下最简单的环境部署-—如何在云服务器部署禅道环境. ...

  7. javascript A*算法 寻路算法 获取最短路径算法

    //A算法 自动寻路 路径 class GetAutoPath{ constructor(id, map, sPos, ePos, mapArr){ //this.type = id.type; th ...

  8. SHELL下打包文件

    SHELL下打包文件 在我们拿下webshell的时候,想要获取数据或者源码往往会用菜刀或者蚁剑去打包,但是这个时候往往就会出现很多问题,列如打包失败,或者是打包得不完整等等. 这个时候如果对方是wi ...

  9. h5笔记(实战)

    1.margin:auto 水平居中只对block有效,对inline和inline-block都无效 2.text-align:center 内容居中对block和inline-block有效,对i ...

  10. js模拟post提交表单

    function post(URL, PARAMS) {            var temp = document.createElement("form");         ...