springAOP定义AfterThrowing增加处理实例分析本⽂实例讲述了spring AOP定义AfterThrowing增加处理。分享给⼤家供⼤家参考,具体如下:
⼀配置
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-4.0.xsd
/schema/context
/schema/context/spring-context-4.0.xsd
/schema/aop
/schema/aop/spring-aop-4.0.xsd">
<!-- 指定⾃动搜索Bean组件、⾃动搜索切⾯类 -->
<context:component-scan
base-package="azyit.app.service
,azyit.app.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
<!-- 启动@AspectJ⽀持 -->
<aop:aspectj-autoproxy />
</beans>
⼆切⾯类
azyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定义⼀个切⾯
@Aspect
public class RepairAspect
{
// 匹配azyit.app.service.impl包下所有类的、
// 所有⽅法的执⾏作为切⼊点
@AfterThrowing(throwing="ex"
, pointcut="execution(* azyit.app.service.impl.*.*(..))")
// 声明ex时指定的类型会限制⽬标⽅法必须抛出指定类型的异常
// 此处将ex的类型声明为Throwable,意味着对⽬标⽅法抛出的异常不加限制
public void doRecoveryActions(Throwable ex)
{
System.out.println("⽬标⽅法中抛出的异常:" + ex);
System.out.println("模拟Advice对异常的修复...");
}
spring framework组件}
三接⼝
Hello
azyit.app.service;
public interface Hello {
// 定义⼀个简单⽅法,模拟应⽤中的业务逻辑⽅法
void foo();
// 定义⼀个addUser()⽅法,模拟应⽤中的添加⽤户的⽅法
int addUser(String name, String pass);
}
World
azyit.app.service;
public interface World {
/
/ 定义⼀个简单⽅法,模拟应⽤中的业务逻辑⽅法
public void bar();
}
四实现类
HelloImpl
azyit.app.service.impl;
import org.springframework.stereotype.Component;
azyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello
{
/
/ 定义⼀个简单⽅法,模拟应⽤中的业务逻辑⽅法
public void foo()
{
System.out.println("执⾏Hello组件的foo()⽅法");
}
// 定义⼀个addUser()⽅法,模拟应⽤中的添加⽤户的⽅法
public int addUser(String name , String pass)
{
System.out.println("执⾏Hello组件的addUser添加⽤户:" + name);
if(name.length() < 3 || name.length() > 10)
{
throw new IllegalArgumentException("name参数的长度必须⼤于3,⼩于10!");
}
return 20;
}
}
WorldImpl
azyit.app.service.impl;
import org.springframework.stereotype.Component;
azyit.app.service.*;
@Component("world")
public class WorldImpl implements World {
/
/ 定义⼀个简单⽅法,模拟应⽤中的业务逻辑⽅法
public void bar() {
System.out.println("执⾏World组件的bar()⽅法");
}
}
五测试类
package lee;
import t.*;
import t.support.*;
azyit.app.service.*;
public class BeanTest {
public static void main(String[] args) {
// 创建Spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("l");
Hello hello = Bean("hello", Hello.class);
hello.foo();
hello.addUser("悟空", "7788");
World world = Bean("world", World.class);
world.bar();
}
}
六测试结果
执⾏Hello组件的foo()⽅法
执⾏Hello组件的addUser添加⽤户:悟空
⽬标⽅法中抛出的异常:java.lang.IllegalArgumentException: name参数的长度必须⼤于3,⼩于10!
模拟Advice对异常的修复...
Exception in thread "main" java.lang.IllegalArgumentException:  name参数的长度必须⼤于3,⼩于10!
at  azyit.app.service.impl.HelloImpl.addUser(HelloImpl.java:30)
flect.NativeMethodAccessorImpl.invoke0(Native  Method)
at  flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at  flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at  org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)    at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at
org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at  org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy7.addUser(Unknown Source)
at lee.BeanTest.main(BeanTest.java:26)
更多关于java相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》和《》
希望本⽂所述对⼤家java程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。