Spring Aop 详解之实例

[color=red][b][size=large]Spring Aop例子详解[/size][/b][/color]


[b]jar要求:[/b]
[list]
[spring 3.2 所需jar包;]
[Aspect所需jar包:aspectjrt.jar,aspectjweaver.jar]
[/list]

spring xml配置文件

<?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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- AOP的注解形式 -->
<aop:aspectj-autoproxy />

<bean id="logManage" class="com.xhy.aop.LogManage" />

<!-- <bean id="logManageXml" class="com.xhy.aop.LogManageXML" />-->

<bean id="people" class="com.xhy.aop.People" />

<!-- AOP的xml配置 -->
<!-- <aop:config>-->
<!-- <aop:aspect ref="logManageXml">-->
<!-- <aop:pointcut expression="execution(* *.work(..))" id="pointcut"/>-->
<!-- <aop:before method="doBefore" pointcut-ref="pointcut"/>-->
<!-- <aop:after method="doAfter" pointcut-ref="pointcut"/>-->
<!-- <aop:around method="doAround" pointcut-ref="pointcut"/>-->
<!-- <aop:after-returning method="doReturn" pointcut-ref="pointcut"/>-->
<!-- <aop:after-throwing method="doThrow" pointcut-ref="pointcut" throwing="e"/>-->
<!-- </aop:aspect>-->
<!-- </aop:config>-->

</beans>

[color=red]注意:注释的那部分是以xml配置方式运行[/color]

目标类:

package com.xhy.aop;
public class People{

public void work(){
System.out.println("----人需要工作才能生活!----");
int x = 5/0;
}

}


注解实现切面类:

@Aspect
public class LogManage {

private static final String EDP = "execution(* *.work())";

@Pointcut(EDP)
public void pointMethod(){}

@Before(EDP)
public void doBefore(){
System.out.println("方法之前做一些事情~~~");
}

@After(EDP)
public void doAfter(){
System.out.println("方法之后做一些事情~~~");
}

@AfterReturning(EDP)
public void doReturn(){
System.out.println("方法返回后做一些事情~~~");
}

@Around(EDP)
public Object doAround(ProceedingJoinPoint joinPoint){
System.out.println("logAround开始:现在时间是:"+new Date());
System.out.println(joinPoint.getClass().getName());
Object[] args = joinPoint.getArgs();
Object obj = null;
try {
obj = joinPoint.proceed(args);
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("logAround结束:现在时间是:"+new Date());
return obj;
}

@AfterThrowing(pointcut="execution(* com.xhy.aop.People.work())",throwing="e")
public void doThrow(Exception e){
if(e!=null){
System.out.println("执行异常:" + e.getMessage());
sendMsg();
}
}

private void sendMsg(){
System.out.println("发送出错报告(邮件、短信)");
}

}

XML实现切面类:

public class LogManageXML {



public void doBefore(){
System.out.println("方法之前做一些事情~~~");
}

public void doAfter(){
System.out.println("方法之后做一些事情~~~");
}

public void doReturn(){
System.out.println("方法返回后做一些事情~~~");
}

public Object doAround(ProceedingJoinPoint joinPoint){
System.out.println("logAround开始:现在时间是:"+new Date());
Object[] args = joinPoint.getArgs();
Object obj = null;
try {
obj = joinPoint.proceed(args);
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("logAround结束:现在时间是:"+new Date());
return obj;
}

public void doThrow(Exception e){
if(e!=null){
System.out.println("执行异常:" + e.getMessage());
sendMsg();
}
}

private void sendMsg(){
System.out.println("发送出错报告(邮件、短信)");
}

}


测试类:

public class AopTest {

public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("/springConfig/applicationContext_AOP.xml");
People people = (People)context.getBean("people");
people.work();
}
}


运行AopTest查看控制台输出的结果。
我在以注解方式运行的时候发现一个问题,即执行5/0的时候抛出异常。@AfterThrowing标记的方法不执行,xml配置方式完全正常。我也没有找到是什么问题。
网上说是@AfterThrowing 和@Around顺序问题;还有代码中需要直接抛出异常,我都试过,不行。所以这算是个bug吧!如果有哪位高手知道,可以交流下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值