Spring源码学习(十六)--自定义切面使用XML配置文件进行Aop测试
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring源码学习(十六)--自定义切面使用XML配置文件进行Aop测试,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3788字,纯文字阅读大概需要6分钟。
内容图文
![Spring源码学习(十六)--自定义切面使用XML配置文件进行Aop测试](/upload/InfoBanner/zyjiaocheng/606/00dc778891bc4d4fb58cdf44628189b1.jpg)
自定义切面使用XML配置文件进行Aop测试
一.普通切面
1. 目标对象
package com.xizi.service;
public interface UserService {
public void add(String name, int age);
public void update(String name, int age);
}
package com.xizi.service;
public class UserServiceImpl implements UserService {
public void add(String name, int age) {
System.out.println("增加了一个用户111111111");
}
public void update(String name, int age) {
System.out.println("更新了一个用户11111111111111");
System.out.println("==抛出异常==:" + 1 / 0);
}
}
2. 切面类
package com.xizi.diy;
import org.aspectj.lang.ProceedingJoinPoint;
public class DiyPointCut {
/**
* 前置增强
*/
public void beforeAdvice(String name, int age) {
System.out.println("==前置增强,name:" + name + ",age:" + age);
}
/**
* 后置异常增强
*/
public void afterExceptionAdvice(String name, int age) {
System.out.println("==后置异常增强,name:" + name + ",age:" + age);
}
/**
* 后置返回增强
*/
public void afterReturningAdvice(String name, int age) {
System.out.println("==后置返回增强,name:" + name + ",age:" + age);
}
/**
* 后置最终增强
*/
public void afterAdvice(String name, int age) {
System.out.println("==后置最终增强,name:" + name + ",age:" + age);
}
//环绕增强
public Object roundAdvice(ProceedingJoinPoint p, String name, int age) {
System.out.println("==环绕增强开始,name:" + name + ",age:" + age);
Object o = null;
try {
o = p.proceed();
Object[] args = p.getArgs();
if (null != args) {
for (int i = 0; i < args.length; i++) {
System.out.println("==环绕增强参数值:" + args[i]);
}
}
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("==环绕增强结束,name:" + name + ",age:" + age);
return o;
}
}
3. 配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--方式二:自定义类-->
<!-- 目标对象 -->
<bean id="userService" class="com.xizi.service.UserServiceImpl"/>
<!-- 自定义切面类-->
<bean id="diy" class="com.xizi.diy.DiyPointCut"/>
<!--AOP配置-->
<aop:config proxy-target-class="true">
<!--自定义切面 ref要引用的类-->
<aop:aspect ref="diy" order="0">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.xizi.service.*.*(..)) and args(name,age)"/>
<!--通知-->
<!--前置增强,在切入点选择的方法之前执行-->
<aop:before method="beforeAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
<!--后置异常增强,在切入点选择的方法抛出异常时执行-->
<aop:after-throwing method="afterExceptionAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
<!--后置返回增强,在切入点选择的方法正常返回时执行-->
<aop:after-returning method="afterReturningAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
<!--后置最终增强,在切入点选择的方法返回时执行,不管是正常返回还是抛出异常都执行-->
<aop:after method="afterAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
<aop:around method="roundAdvice" pointcut-ref="pointcut" arg-names="p,name,age"/>
</aop:aspect>
</aop:config>
</beans>
4. 测试类及结果
测试这个不带异常的方法
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add("戏子",123);
}
测试这个带异常的方法
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.update("戏子",123);
}
内容总结
以上是互联网集市为您收集整理的Spring源码学习(十六)--自定义切面使用XML配置文件进行Aop测试全部内容,希望文章能够帮你解决Spring源码学习(十六)--自定义切面使用XML配置文件进行Aop测试所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。