环绕通知(xml)
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)的更多相关文章
- 通知类型 重点: 环绕通知 (XML配置)
前置通知:在切入点方法执行之前执行 <aop:before method="" pointcut-ref="" ></aop:before&g ...
- spring 切面 前置后置通知 环绕通知demo
环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- spring框架应用系列四:切面编程(环绕通知与前后置通知区别)
切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...
- 基于XML的AOP配置(2)-环绕通知
配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*( ...
- spring aop 环绕通知around和其他通知的区别
前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
- 14Spring_AOP编程(AspectJ)_环绕通知
在目标方法执行前后,进行代码增强 (阻止目标方法的执行 ) 环绕通知实现任何通知效果. 案例如下: 案例结构:
- spring aop环绕通知
[Spring实战]—— 9 AOP环绕通知 假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...
- [转载] spring aop 环绕通知around和其他通知的区别
前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
- aop编程之后置通知,环绕通知和异常通知
---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html 具体流程如下: 1. ...
随机推荐
- 《 Java 编程思想》CH05 初始化与清理
< Java 编程思想>CH05 初始化与清理 用构造器确保初始化 在 Java 中,通过提供构造器,类的设计者可确保每个对象都会得到初始化.Java 会保证初始化的进行.构造器采用与类相 ...
- Windows API 教程(九) 网络编程
茵蒂克丝 基础概念 ip 地址 服务端与客户端 Socket 基础概念 头文件和库文件 常用函数 WSAStartup ( ) 函数 WSACleanup ( ) 函数 Socket ( ) 函数 c ...
- linux笔记之解压
从1.15版本开始tar就可以自动识别压缩的格式,故不需人为区分压缩格式就能正确解压: Linux下常见的压缩包格式有5种:zip tar.gz tar.bz2 tar.xz tar.Z 其中tar是 ...
- 学习CSS之用CSS绘制一些基本图形
一.三角形 如下图,通过设置 border 的大小和颜色可以形成四个三角形: 上图对应的代码为: /* 三角形 */ .triangle { width: 0; height: 0; ...
- WeChall_Training: Programming 1 (Training, Coding)
When you visit this link you receive a message.Submit the same message back to http://www.wechall.ne ...
- lua学习之基础概念篇
基础概念 程序块 (chunk) 定义 lua 中的每一个源代码文件或在交互模式(Cmd)中输入的一行代码都称之为程序块 一个程序块就是一连串语句或者命令 lua 中连续的语句不需要分隔符,但为了可读 ...
- 一个新实验:使用gRPC-Web从浏览器调用.NET gRPC服务
今天给大家翻译一篇由ASP.NET首席开发工程师James Newton-King前几天发表的一篇博客,文中带来了一个实验性的产品gRPC-Web.大家可以点击文末的讨论帖进行相关反馈.我会在文章末尾 ...
- Codevs 1205 单词反转(Vector以及如何输出string)
题意:倒序输出句子中的单词 代码: #include<cstdio> #include<iostream> #include<string> #include< ...
- js 浏览器兼容问题及解决办法
JS中出现的兼容性问题的总结 1.关于获取行外样式 currentStyle 和 getComputedStyle 出现的兼容性问题 我们都知道js通过style不可以获取行外样式,当我们需要获取行 ...
- Python实现IOC控制反转
思路: 用一个字典存储beanName和资源 初始化时先将beanName和资源注册到字典中 然后用一个Dscriptor类根据beanName动态请求资源,从而实现控制反转 # -*- coding ...