Spring Learning Journey (vii) comparison of AOP programming based on XML configuration and ASPECTJ annotation configuration

Source: Internet
Author: User
Tags aop arithmetic mul

This blog post uses a slightly more complex case to compare the differences between XML-based configuration and AOP programming based on ASPECTJ annotation configuration.

For the preparation of spring AOP programming such as the introduction package, please refer to the other blog posts in the small series.

Case Requirements:

Write a simple calculator that implements arithmetic.

Add AOP Function: Log function, check whether there is negative function in parameter.

Nonsense not much to say, directly on the code:

(i) XML-based configuration:

Defines an interface class:

 Package COM.EDU.AOP;  Public Interface arithmeticcalculator {    int Add ( int i,int  j);     int Sub (int i,int  j);     int mul (int i,int  j);     int div ( int i,int  j);}

Implementation class:

 PackageCOM.EDU.AOP; Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {returni+J; } @Override Public intSubintIintj) {returnI-J; } @Override Public intMulintIintj) {returni*J; } @Override Public intDivintIintj) {returni/J; }}

Log Slice class:

 PackageCOM.EDU.AOP;Importjava.util.Arrays;ImportOrg.aspectj.lang.JoinPoint; Public classLoggingaspect {/*** Log Slice class*/         Public voidBeforemethod (Joinpoint joinpoint) {//Get method NameString methodname=joinpoint.getsignature (). GetName (); //get a list of method argument valuesObject[] Args=Joinpoint.getargs (); System.out.println ("The method" +methodname+ "begin with" +arrays.aslist (args)); }         Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "ends"); }}

The slice class that detects if there are negative numbers in the parameter:

 PackageCOM.EDU.AOP;Importjava.util.Arrays;ImportOrg.aspectj.lang.JoinPoint; Public classValidationaspect { Public voidValidationargs (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); Object[] args=Joinpoint.getargs (); System.out.println ("Parameters to verify:" +arrays.aslist (args)); if(args!=NULL&&args.length>0){             for(inti=0;i<args.length;++i) {                if(((Integer) args[i]). Intvalue () <0) {System.out.println ("Warning: Method" +methodname+ "()" + (i+1) + "parameter is negative:" +Args[i]); }            }        }    }}

XML configuration file:

<?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"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "><!--Configuration Bean -<BeanID= "Arithmetic"class= "Com.edu.aop.ArithmeticCalculatorImpl"></Bean><!--Configure the Slice class declaration as a bean, respectively -<BeanID= "Logging"class= "Com.edu.aop.LoggingAspect"></Bean><BeanID= "Validation"class= "Com.edu.aop.ValidationAspect"></Bean><Aop:config><!--Configuring the Log tile class -<Aop:aspectref= "Logging"><!--Configure a pointcut for pre-notification and pre-notification -<Aop:beforeMethod= "Beforemethod"pointcut= "Execution (* com.edu.aop.arithmeticcalculatorimpl.* (..))"></Aop:before><!--Configure the entry point for post notification and post notification -<Aop:afterMethod= "Aftermethod"pointcut= "Execution (* com.edu.aop.arithmeticcalculatorimpl.* (..))"></Aop:after></Aop:aspect><!--Configuring the detection parameters slicing class -<Aop:aspectref= "Validation"><!--Configure a pointcut for pre-notification and pre-notification -<Aop:beforeMethod= "Validationargs"pointcut= "Execution (* com.edu.aop.arithmeticcalculatorimpl.* (..))"></Aop:before></Aop:aspect></Aop:config></Beans>

Main method Detection class:

 PackageCOM.EDU.AOP;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {ApplicationContext act=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Arithmeticcalculator arithmetic= (arithmeticcalculator) Act.getbean ("Arithmetic"); intResult=arithmetic.add (10, 20); System.out.println ("Result:" +result); Result=arithmetic.div (-36, 4); System.out.println ("Result:" +result); }}

Operation Result:

(ii) AOP programming based on the ASPECTJ annotation configuration:

Not to be continued.

Spring Learning Journey (vii) comparison of AOP programming based on XML configuration and ASPECTJ annotation configuration

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.