Spring AOP(4)

本文详细介绍了如何在Spring框架中使用XML配置实现面向切面编程(AOP),涵盖了定义业务Bean、切面、配置切入点及各种通知类型的方法,并通过实例展示了不同场景下通知的执行顺序。
在第三节里面,完满讲了使用@AspectJ注解实现Spring AOP,它需要运行在Java5以上的版本中,对于Java1.4之前的版本,我们也想使用Spring AOP,那么怎么办呢?
一种是像1,2节里面讲的那样,定义Advice实现MethodBeforeAdvice、MethodAfterAdvice、ThrowsAdvice、MethodInterceptor接口之一,然后包装在Advisor中,最后使用BeanPostProcessor(如BeanNameAutoProxyCreator、DefaultAdvisorAutoProxyCreator)创建业务Bean的代理对象。显然这种方式很繁琐。
第二种选择是使用Spring的<aop:...>命名空间,这是在2.0版本以后引入的。它可以运行在Java1.4基础上。本节主要介绍使用<aop:...>基于xml配置实现Spring AOP。
1、定义业务Bean

package spring.aop;
public class UserService {
public void getUser(int id){
System.out.println("the user id: "+id);
}
}

2、定义切面

public class UserAspect {
//业务方法执行前会执行此操作
public void before() {
System.out.println("before advice");
}
//业务方法正常执行结束后会执行此操作
public void afterReturn() {
System.out.println("after-returning advice");
}
//相当于finally,无论业务方法是否产生异常,执行后都会执行此操作
public void after() {
System.out.println("after advice");
}
}

这里的切面就是一个简单的POJO,不需要实现任何接口,不需要继承任何类。里面的方法就是一个Advice(包括before、after、after-throwing、after-return、around五种类型),而Pointcut在xml配置。
3、配置xml

<bean id="userService" class="spring.aop.UserService"/>
<bean id="aspect" class="spring.aop.UserAspect"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* spring.aop.UserService.* (..))"/>
<aop:aspect ref="aspect">
<aop:before pointcut-ref="pointcut" method="before"/>
<aop:after-returning pointcut-ref="pointcut" method="afterReturn" >
<aop:after pointcut-ref="pointcut" method="after">
</aop:aspect>
</aop:config>


测试代码:

ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us=(UserService)con.getBean("userService");
us.getUser(12);

输出结果:
before advice
the user id: 12
after-returning advice
after advice

4、环绕通知

public Object around(ProceedingJoinPoint pjp) throws Throwable{
try{
System.out.println("around advice: before");
Object obj=pjp.proceed();//必须调用此方法,否则后续处理终断
System.out.println("around advice: after-returning");
return obj;
}catch(Exception e){
throw e;
}
}

对应的xml配置:

<aop:aspect ref="aspect">
<aop:around pointcut-ref="pointcut" method="around"/>
</aop:aspect>

环绕通知使我们有机会在方法执行的前后都作出相应的处理,功能最为强大,但必须包含一个处理连接点参数ProceedingJoinPoint pjp,并在与处理(before)之后调用pjp.proceed(),忘记次调用会带来莫名其妙的结果,所以能使用其他Advice进行处理的,尽量使用其他更简单的Advice。

5、异常

public void exception(Exception e) {
//记录异常
System.out.println("exception ["+e+"]");
}

这里参数Exception e是必须的。在xml配置中也必须指明此参数:throwing="e"
对应的xml配置:

<aop:aspect ref="aspect">
<aop:after-throwing pointcut-ref="pointcut" method="exception" throwing="e"/>
</aop:aspect>

6、使用带参数的Advice
上面的例子中除了around和after-throwing含有参数,且around中的参数不需要xml中配置,其他的Advice都是无参数的,要想使用带有自定义参数的Advice,怎么办呢?此时就需要重新配置Pointcut了:
例子,一个带参数的before Advice:
定义业务Bean:

public class UserService {
public void login(User user){
System.out.println("user id: "+u.getId());
}
}

定义before Advice:

public void before(User user) {
System.out.println("user "+u.getName()+"try to login...");
}

在xml中配置Pointcut:

<aop:config>
<aop:pointcut id="pointcut" expression="execution(* spring.aop.UserService.* (User)) and args(u)"/>
<aop:aspect ref="aspect">
<aop:before pointcut-ref="pointcut" method="before" arg-names="u"/>
</aop:aspect>
</aop:config>

关键在于这一行:expression="execution(* spring.aop.UserService.* (User)) and args(u)",指明了切入点为spring.aop.UserService的任何方法,[b][color=red]并且[/color][/b]此方法含有一个类型的User的参数,参数名为u(可以与业务Bean中的参数名不一样,实际上是它的一个别名),<aop:before../>中arg-names就是引用的这个别名(可以与Advice中的参数名不一样)。

[color=red]注意:
其他Advice不能共享此Pointcut,除非Advice中的参数与此Pointcut中的参数一致。[/color]


有人会问,如果Advice中只使用业务Bean方法的[color=red][b]部分参数[/b][/color],该如何做呢?
答案是:依然利用Pointcut配置。
Demo:
定义业务Bean:

public class UserService {
public void login(String name,String psw){
System.out.println("login...");
}
}

定义before Advice:

public void before(String n) {
System.out.println("user "+n+" try to login...");
}

在xml中配置Pointcut:

<aop:config>
<aop:pointcut id="pointcut" expression="execution(* spring.aop.UserService.* (String,..)) and args(n,..)"/>
<aop:aspect ref="aspect">
<aop:before pointcut-ref="pointcut" method="before" arg-names="n"/>
</aop:aspect>
</aop:config>

(String,..)声明切入点至少含有一个String类型的参数,显然可以匹配UserService中的login(String name,String psw);
args(n,..)声明给login(String name,String psw)的第一个参数起了个别名“n”传递给Advice,如果<aop:before...>中arg-names不是“n”,将抛出异常。

7、Advice的顺序
1)一般情况下,before、after-throwing、after的执行顺序是一定的,即:
before-->after-throwing-->after
而before与around中的proceed()方法调用之前的处理则是按照谁配在前谁先处理的原则,after与around中的proceed()方法调用之后的处理也是如此;
当异常抛出时,after-returning操作不会被处理,而after-throwing、after依次被处理。
2)如果是基于注解的方式,在测试中发现是按照如下顺序执行增强的:
before advice
around advice: before
login...
around advice: after-returning
after-returning advice
after advice
下载前可以先看下教程 https://pan.quark.cn/s/a426667488ae 标题“仿淘宝jquery图片左右切换带数字”揭示了这是一个关于运用jQuery技术完成的图片轮播机制,其特色在于具备淘宝在线平台普遍存在的图片切换表现,并且在整个切换环节中会展示当前图片的序列号。 此类功能一般应用于电子商务平台的产品呈现环节,使用户可以便捷地查看多张商品的照片。 说明中的“NULL”表示未提供进一步的信息,但我们可以借助标题来揣摩若干核心的技术要点。 在构建此类功能时,开发者通常会借助以下技术手段:1. **jQuery库**:jQuery是一个应用广泛的JavaScript框架,它简化了HTML文档的遍历、事件管理、动画效果以及Ajax通信。 在此项目中,jQuery将负责处理用户的点击动作(实现左右切换),并且制造流畅的过渡效果。 2. **图片轮播扩展工具**:开发者或许会采用现成的jQuery扩展,例如Slick、Bootstrap Carousel或个性化的轮播函数,以达成图片切换的功能。 这些扩展能够辅助迅速构建功能完善的轮播模块。 3. **即时数字呈现**:展示当前图片的序列号,这需要通过JavaScript或jQuery来追踪并调整。 每当图片切换时,相应的数字也会同步更新。 4. **CSS美化**:为了达成淘宝图片切换的视觉效果,可能需要设计特定的CSS样式,涵盖图片的排列方式、过渡效果、点状指示器等。 CSS3的动画和过渡特性(如`transition`和`animation`)在此过程中扮演关键角色。 5. **事件监测**:运用jQuery的`.on()`方法来监测用户的操作,比如点击左右控制按钮或自动按时间间隔切换。 根据用户的交互,触发相应的函数来执行...
垃圾实例分割数据集 一、基础信息 • 数据集名称:垃圾实例分割数据集 • 图片数量: 训练集:7,000张图片 验证集:426张图片 测试集:644张图片 • 训练集:7,000张图片 • 验证集:426张图片 • 测试集:644张图片 • 分类类别: 垃圾(Sampah) • 垃圾(Sampah) • 标注格式:YOLO格式,包含实例分割的多边形点坐标,适用于实例分割任务。 • 数据格式:图片文件 二、适用场景 • 智能垃圾检测系统开发:数据集支持实例分割任务,帮助构建能够自动识别和分割图像中垃圾区域的AI模型,适用于智能清洁机器人、自动垃圾桶等应用。 • 环境监控与管理:集成到监控系统中,用于实时检测公共区域的垃圾堆积,辅助环境清洁和治理决策。 • 计算机视觉研究:支持实例分割算法的研究和优化,特别是在垃圾识别领域,促进AI在环保方面的创新。 • 教育与实践:可用于高校或培训机构的AI课程,作为实例分割技术的实践数据集,帮助学生理解计算机视觉应用。 三、数据集优势 • 精确的实例分割标注:每个垃圾实例都使用详细的多边形点进行标注,确保分割边界准确,提升模型训练效果。 • 数据多样性:包含多种垃圾物品实例,覆盖不同场景,增强模型的泛化能力和鲁棒性。 • 格式兼容性强:YOLO标注格式易于与主流深度学习框架集成,如YOLO系列、PyTorch等,方便研究人员和开发者使用。 • 实际应用价值:直接针对现实世界的垃圾管理需求,为自动化环保解决方案提供可靠数据支持,具有重要的社会意义。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值