Spring's surround Notification

Source: Internet
Author: User
Tags mul stub

First add the jar package:

Com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0. jar
com.springsource.org.aspectj.weaver-1.6.8. Release.jar
commons-logging-1.1.3. jar
Spring-aop-4.0.0.release.jar
Spring-aspects-4.0.0.release.jar
Spring-beans-4.0.0.release.jar
Spring-context-4.0.0.release.jar
Spring-core-4.0.0.release.jar
Spring-expression-4.0.0.release. jar

Declare an interface:

 public  interface   MyMath { public  int  Add (    Span style= "COLOR: #0000ff" >int  i,int   J);  public  void  Sub (int  i,int   J);  public  void  mul (int  i,int   J);  public  void  div (int  i,int   J);}  

Creating a class implementation interface

@Component Public classMymathimplImplementsmymath{@Override Public intAddintIintj) {intresult=i+J; System.out.println ("Target method Add executed," +result); returnresult; } @Override Public voidSubintIintj) {//TODO auto-generated Method Stub        intresult=i-J; System.out.println ("The target method Sub executes," +result); } @Override Public voidMulintIintj) {//TODO auto-generated Method Stub        intresult=i*J; System.out.println ("Target method Mul executed," +result); } @Override Public voidDivintIintj) {//TODO auto-generated Method Stub        intresult=i/J; System.out.println ("The target method Div executes," +result); }}

Configuring scan packages and opening AOP annotations in Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd ">          <!--Scan Package -     <Context:component-scanBase-package= "Com.neuedu.bean"></Context:component-scan> <!--Turn on AOP annotations -    <Aop:aspectj-autoproxy></Aop:aspectj-autoproxy></Beans>

Create slice classes that surround notifications

@Component @aspect Public classTxaspect {@Around (value= "Execution (public * com.neuedu.bean.mymathimpl.* (..))")     PublicObject Showlog (proceedingjoinpoint point) {object[] args=Point.getargs (); Object Object=NULL; Try{Object=point.proceed (args); } Catch(Throwable E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } List<Object> aslist =arrays.aslist (args); Signature Signature=point.getsignature (); String name=Signature.getname (); Try {            Try{System.out.println ("Transaction log", "Pre-Notification", parameter: "+ Name +" Method: "+aslist); } finally{System.out.println ("Transaction log", "Post notification", parameter: "+ Name +" Method: "+aslist); } System.out.println ("Transaction log", "return notification", parameter: "+ Name +" Method: "+aslist); } Catch(Exception e) {//Todo:handle ExceptionSYSTEM.OUT.PRINTLN ("Transaction log", "Exception notification", parameter: "+ Name +" Method: "+aslist); }        returnobject; }}

Surround notification Be sure to return the value of the object, otherwise the content body of the target method will not be executed, i.e. it will not enter the implementation class method body.

Compared to the configured four notifications, the form of the surround notification is:

try{

try{

Front-facing notifications

}

finally{

Post notification

}

Back to Notifications

}

catch{

Exception notification

}

Spring's surround Notification

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.