Simple example of "spring-aop-Learning note-6" @AfterThrowing enhancement processing

Source: Internet
Author: User

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;
  
 
  1. @Aspect
  2. public class RepairAspect
  3. {
  4. // 匹配org.crazyit.app.service.impl包下所有类的、
  5. // 所有方法的执行作为切入点
  6. @AfterThrowing(throwing="ex"
  7. , pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
  8. // 声明ex时指定的类型会限制目标方法必须抛出指定类型的异常
  9. // 此处将ex的类型声明为Throwable,意味着对目标方法抛出的异常不加限制
  10. public void doRecoveryActions(Throwable ex)
  11. {
  12. System.out.println("目标方法中抛出的异常:" + ex);
  13. System.out.println("模拟Advice对异常的修复...");
  14. }
  15. }

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

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.