Spring learning journey (7) Comparison of AOP Programming Based on XML configuration and AspectJ annotation configuration, aspectjaop

Source: Internet
Author: User

Spring learning journey (7) Comparison of AOP Programming Based on XML configuration and AspectJ annotation configuration, aspectjaop

This blog post compares XML-based configuration with AOP Programming Based on AspectJ annotation configuration with a case with a slightly more complex point.

For more information about Spring AOP programming preparations such as the Introduction package, see other blog posts in the small series.

Case requirements:

Write a simple calculator that implements four arithmetic operations.

Add the AOP function: log function; check for negative values in parameters.

If you don't talk much about it, go directly to the Code:

(1) 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:

package com.edu.aop;public class ArithmeticCalculatorImpl implements ArithmeticCalculator {    @Override    public int add(int i, int j) {        return i+j;    }    @Override    public int sub(int i, int j) {        return i-j;    }    @Override    public int mul(int i, int j) {        return i*j;    }    @Override    public int div(int i, int j) {        return i/j;    }}

Log slicing:

Package com.edu. aop; import java. util. arrays; import org. aspectj. lang. joinPoint; public class LoggingAspect {/*** log partition class */public void beforeMethod (JoinPoint joinPoint) {// obtain method name String methodName = joinPoint. getSignature (). getName (); // get method real parameter value list Object [] args = joinPoint. getArgs (); System. out. println ("The method" + methodName + "begin with" + Arrays. asList (args);} public void afterMethod (JoinPoint joinPoint) {String methodName = joinPoint. getSignature (). getName (); System. out. println ("The method" + methodName + "ends ");}}

Check whether there are negative slice types in the parameters:

Package com.edu. aop; import java. util. arrays; import org. aspectj. lang. joinPoint; public class ValidationAspect {public void validationArgs (JoinPoint joinPoint) {String methodName = joinPoint. getSignature (). getName (); Object [] args = joinPoint. getArgs (); System. out. println ("parameters to be verified:" + Arrays. asList (args); if (args! = Null & args. length> 0) {for (int I = 0; I <args. length; ++ I) {if (Integer) args [I]). intValue () <0) {System. out. println ("Warning: Method" + methodName + "() number" + (I + 1) + "parameters are negative: "+ args [I]) ;}}}

Xml configuration file:

<? 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <! -- Configure bean --> <bean id = "arithmetic" class = "com.edu. aop. ArithmeticCalculatorImpl"> </bean> <! -- Configure the partition class declaration as a bean --> <bean id = "logging" class = "com.edu. aop. loggingAspect "> </bean> <bean id =" validation "class =" com.edu. aop. validationAspect "> </bean> <aop: config> <! -- Configure the log slicing class --> <aop: aspect ref = "logging"> <! -- Configure the start point of the pre-notification and pre-notification --> <aop: before method = "beforeMethod" pointcut = "execution (* com.edu. aop. arithmeticCalculatorImpl. *(..)) "> </aop: before> <! -- Configure the entry point for post-notification and post-notification --> <aop: after method = "afterMethod" pointcut = "execution (* com.edu. aop. arithmeticCalculatorImpl. *(..)) "> </aop: after> </aop: aspect> <! -- Configure the detection parameter slicing class --> <aop: aspect ref = "validation"> <! -- Configure the start point for pre-notifications and pre-notifications --> <aop: before method = "validationArgs" pointcut = "execution (* com.edu. aop. arithmeticCalculatorImpl. *(..)) "> </aop: before> </aop: aspect> </aop: config> </beans>

Main method detection class:

package com.edu.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {        ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");        ArithmeticCalculator arithmetic=(ArithmeticCalculator)act.getBean("arithmetic");        int result=arithmetic.add(10, 20);        System.out.println("result:"+result);        result=arithmetic.div(-36, 4);        System.out.println("result:"+result);    }}

Running result:

(2) AOP Programming Based on AspectJ annotation Configuration:

To be continued.

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.