Project structure
Business code @Component("Hello") public class Helloimpl implements Hello { //Define a simple method to simulate the business logic approach in the application Public void foo () {System. Out. println ("foo () method to execute Hello component"); } //define a AddUser () method to simulate the method of adding users in the app Public int addUser (string name, String pass) {System. Out. println ("Execute the adduser add user for Hello component:" + name); If(Name.length () < 3 | | name.length () >) { throw new illegalargumentexception ("the name parameter must be longer than 3, less than 10!") "); } return ; }}
@Component("World") public class Worldimpl implements World{ //Define a simple method to simulate the business logic approach in the application Public void bar () {System. Out. println ("bar () method to execute World component"); }}
Defining Slice BeansDescription:If the type of ex is nullpointer-exception, only the exception of that type is matched;
@Aspect
public class RepairAspect
{
// 匹配org.crazyit.app.service.impl包下所有类的、
// 所有方法的执行作为切入点
@AfterThrowing(throwing="ex"
, pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
// 声明ex时指定的类型会限制目标方法必须抛出指定类型的异常
// 此处将ex的类型声明为Throwable,意味着对目标方法抛出的异常不加限制
public void doRecoveryActions(Throwable ex)
{
System.out.println("目标方法中抛出的异常:" + ex);
System.out.println("模拟Advice对异常的修复...");
}
}
Configuration <? XML version="1.0" encoding="GBK"?> <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-4.0.xsdHttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd "> <!--specify automatic search for bean components, automatic search of slice classes -- <context:component-scan base-package="Org.crazyit.app.service , Org.crazyit.app.aspect "> <context:include-filter type="annotation" Expression="Org.aspectj.lang.annotation.Aspect"/> </context:component-scan> <!--start @aspectj support -- <aop:aspectj-autoproxy/> </Beans>
Test public class beantest {Public static void main (string[] args) { //Create a spring container ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml");Hello hello = ctx.getbean ("Hello" , hello. Class);Hello.foo (); Hello.adduser ("Goku", "7788");//Throws an exception--the length of the name is not enoughWorld World = Ctx.getbean (' World ' , world. Class);World.bar (); }}
Note: As can be seen from the above, although afterthrowing can handle the exception of the target method, this processing method differs from the direct use of catch capture, as follows:
- Catch: means that the exception is fully handled, and if no new exception is re-thrown in the catch statement, the method can end normally;
- Afterthrowing: Although the exception is handled, it cannot handle the exception completely, and the exception is still propagated to the caller at the previous level.
From for notes (Wiz)
List of attachments
Simple example of "spring-aop-Learning note-6" @AfterThrowing enhancement processing