Aop implementation Interface Method

Source: Internet
Author: User

Before advice: A notification executed Before a connection point. However, this notification cannot prevent the execution process Before the connection point (unless it throws an exception ).
After returning advice: a notification that is executed After a connection point is completed normally. For example, a method does not throw any exception and returns a normal result.
After throwing advice: the notification that is executed when the method throws an exception and exits.
After (finally) advice: the notification executed when a connection point exits (whether it is a normal return or an abnormal exit ).
Around Advice: a notification that surrounds a connection point, such as a method call. This is the most powerful notification type. Surround notifications can complete custom actions before and after method calls. It will also choose whether to continue executing the connection point or directly return its own return value or throw an exception to end the execution.
1. Surround notification:
[Java]
Public class LogInterceptor implements MethodInterceptor {
 
@ Override
Public Object invoke (MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stub
System. out. println ("aop begin ");
Object obj = arg0.proceed ();
System. out. println ("aop end ");
Return obj;
}
}

2. pre-notification:
[Java]
Public class BeforInterceptor implements MethodBeforeAdvice {
 
@ Override
Public void before (Method arg0, Object [] arg1, Object arg2)
Throws Throwable {
// TODO Auto-generated method stub
System. out. println ("BeforInterceptor ");
System. out. println ("Entry Point Object:" + arg2 + "parameter:" + arg1 [0] + "entry point:" + arg0.getName ());
}
 
}

3. Post-notification:
[Java]
Public class AfterInterceptor implements AfterReturningAdvice {
@ Override
Public void afterReturning (Object arg0, Method arg1, Object [] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
System. out. println (arg0 + "" + arg3 );
}
}
4. Exception notification:
ThrowsAdvice is a label interface. We can define one or more classes to capture exceptions thrown by bean defining exception notifications, and execute corresponding methods before throwing exceptions.
[Java] view plaincopy
Public class ExceptionInterceptor implements ThrowsAdvice {
Public void afterThrowing (Exception e ){
System. out. println (e );
System. out. println ("ExceptionInterceptor Exception ");
}
Public void afterThrowing (NullPointerException e ){
System. out. println (e );
System. out. println ("ExceptionInterceptor NullPointerException ");
}
}
After an Exception notification is intercepted, no other methods will be executed. For example, if NullPointerException is intercepted, no Exception will be intercepted.

Xml:
[Html]
<Aop: config>
<Aop: pointcut expression = "execution (* org. han. service .*.*(..))"
Id = "logpoint"/>
<! -- Surround notification -->
<Aop: advisor advice-ref = "loginter1" pointcut-ref = "logpoint"/>
 
<! -- Pre-notification -->
<Aop: advisor advice-ref = "loginter2" pointcut-ref = "logpoint"/>
 
<! -- Post-notification -->
<Aop: advisor advice-ref = "loginter3" pointcut-ref = "logpoint"/>

<! -- Exception notification -->
<Aop: advisor advice-ref = "loginter4" pointcut-ref = "logpoint"/>
</Aop: config>

Services:
[Java]
Public class LoginService {
Public void login (){
System. out. println ("yyy logging on ");
User u = null;
System. out. println (u. getUname ());
System. out. println ("xxx is logged on ");
}
Public User register (){
System. out. println ("registered successfully ");
Return new User ("han", "zhou ");
}
Public User getUser (String uname, String pwd ){
System. out. println ("getUser ");
Return new User (uname, pwd );
}
Public void login (String uname ){
System. out. println (uname + "logging in ");
}
}

Author: hanzhou4519

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.