Spring4 Learning Note-AOP (profile-based approach)

Source: Internet
Author: User

The introduced jar package is the same as the jar package introduced in the annotation-based approach

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4A/17/wKiom1QidHCgp74BAADrO2jIId0739.jpg "title=" 1.png " alt= "Wkiom1qidhcgp74baadro2jiid0739.jpg"/>

Arithmeticcalculator interface

Package Com.spring.aop.impl.xml;public interface Arithmeticcalculator {public int add (int i, int j);p Ublic int sub (int i, Int j);p ublic int mul (int i, int j);p ublic int div (int i, int j);}

< /span> interface implementation class  arithmeticcalculatorimpl.java

package com.spring.aop.impl.xml;public class  arithmeticcalculatorimpl implements arithmeticcalculator{@Overridepublic  int add (int  I,&NBSP;INT&NBSP;J)  {int result = i + j;return result;} @Overridepublic  int sub (int i, int j)  {int result = i -  j;return result;} @Overridepublic  int mul (int i, int j)  {int result = i *  j;return result;} @Overridepublic  int div (int i, int j)  {int result = i /  j;return result;}} 

< /span> slice class  loggingaspect.java

package com.spring.aop.impl.xml;import java.util.arrays;import java.util.list;import  org.aspectj.lang.joinpoint;import org.aspectj.lang.proceedingjoinpoint;public class  Loggingaspect {public void beforemethod (Joinpoint joinpoint)  {String methodName  = joinpoint.getsignature (). GetName (); List<object>args = arrays.aslist (Joinpoint.getargs ()); System.out.println ("Pre-notification:the method " + methodname + " begins with "  +  args);} Public void aftermethod (Joinpoint joinpoint)  {String methodName =  Joinpoint.getsignature (). GetName ();//list<object>args = arrays.aslist (Joinpoint.getargs ());    in the Post notification method, you can get to the parameter System.out.println ("Post-notification:the method " + methodname + " ends   ");} Public void afterreturnning (Joinpoint joinpoint, object result)  {String  methodname  = joinpoint.getsignature (). GetName (); SYSTEM.OUT.PRINTLN ("Return notification:the method " + methodname + " ends with "  +  result);} Public void afterthrowing (joinpoint joinpoint, exception e)  {String  Methodname = joinpoint.getsignature (). GetName (); SYSTEM.OUT.PRINTLN ("Exception notification:the method " + methodname + " occurs exception " &NBSP;+&NBSP;E);} Public object aroundmethod (Proceedingjoinpoint point)  {object result = null ; String methodname = point.getsignature (). GetName () try {//Pre-notification System.out.println ("the  method  "+ methodname +"  begins with  " + arrays.aslist" ( Point.getargs ()));//Execute Target Method Result = point.proceed ();//Translation Notification System.out.println ("the method " +  methodname + " ends with "  + result);}  catch  (throwable e)  {//exception Notification SysteM.out.println ("the method " + methodname + " occurs exception "  + e ); Throw new runtimeexception (e);} Post-Notification System.out.println ("the method " + methodname + " ends"); return result;}}

Slice class Validationaspect.java

Package Com.spring.aop.impl.xml;import Java.util.arrays;import Org.aspectj.lang.joinpoint;public class validationaspect {public void Validateargs (Joinpoint joinpoint) {System.out.println ("Validate:" + arrays.aslist ( Joinpoint.getargs ()));}}

Applicationcontext-xml.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:aop=" http://www.springframework.org/ Schema/aop "xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org /schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/ Schema/aop/spring-aop-4.1.xsd "><!--  Configure bean --><bean id=" Arithmeticcalculator "  class= "Com.spring.aop.impl.xml.ArithmeticCalculatorImpl" ></bean><!--  configuration facets bean --> <bean id= "Loggingaspect"  class= "Com.spring.aop.impl.xml.LoggingAspect" ></bean><bean  id= "Validationaspect"  class= "Com.spring.aop.impl.xml.ValidationAspect" ></bean><!--   Configure aop --><aop:config><!--  Configure Pointcut expression  --><aop:pointcut expression= " Execution (* com.spring.aop.impl.xml.arithmeticcalculator.* (..)) "  id= "Pointcut"/><!--  configuration facets and notifications, using order to specify priority  --><aop:aspect ref= "Loggingaspect"  order= "1" ><!--  Surround notification  --><!--  <aop:around method= "Aroundmethod"  pointcut-ref= "Pointcut"/>--><!--  pre-notification  --><aop:before method= "Beforemethod"  pointcut-ref= "Pointcut"/><!--  Post notification  --><aop:after method= "Aftermethod"   pointcut-ref= "Pointcut"/><!--  Exception notification  --><aop:after-throwing method= "Afterthrowing"   pointcut-ref= "Pointcut"  throwing= "E"/><!--  return notification  --><aop:after-returning  method= "afterreturnning"  pointcut-ref= "pointcut"  returning= "Result"/></aop:aspect> <aop:aspect ref= "Validationaspect"  order= "2" ><!--  pre-notification  --><aop:before  Method= "Validateargs"  pointcut-ref= "Pointcut"/></aop:aspect></aop:config></beans> 

main.java

package com.spring.aop.impl.xml;import  org.springframework.context.applicationcontext;import  Org.springframework.context.support.classpathxmlapplicationcontext;public class main {public  static void main (String[] args)  {//create SPRING&NBSP;IOC container applicationcontext  Applicationcontext = new classpathxmlapplicationcontext ("Applicationcontext-xml.xml");// Gets the bean instance Arithmeticcalculator arithmeticcalculator = applicationcontext.getbean from the IOC container ( Arithmeticcalculator.class); Int result = arithmeticcalculator.add (4, 6); SYSTEM.OUT.PRINTLN (result); Result = arithmeticcalculator.sub (4, 6); SYSTEM.OUT.PRINTLN (result); SYSTEM.OUT.PRINTLN (result); Result = arithmeticcalculator.mul (4, 6); SYSTEM.OUT.PRINTLN (result); SYSTEM.OUT.PRINTLN (result); Result = arithmeticcalculator.div (4, 0); SYSTEM.OUT.PRINTLN (result);}} 



SOURCE Http://yunpan.cn/cgsrQHmUvrIQt Extract Code 6564

This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1557743

Spring4 Learning Note-AOP (profile-based approach)

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.