Spring AOP pre-notification and post-notification

Source: Internet
Author: User
Tags mul

    • To add a jar package:

    • To include the AOP namespace in the configuration file

    • Annotation-based annotations are described in the following configuration file:

Beans-aop-helloworld.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" xmlns: context= "Http://www.springframework.org/schema/context" 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.2.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.2.xsd ">    <!--Configuration Auto-Scan Package--    < Context:component-scan base-package= "Spring.aop.helloworld" ></context:component-scan>        <!-- ASPECTJ Annotations Work: Automatically generate proxy objects for matching classes--    <aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans >

    • To abstract the code of crosscutting concerns into a class of facets
      • The slice is first a bean in the IOC, which joins the @component annotation
      • Facets also need to be added to @aspect annotations:
    • Declare various notifications in the class:
      • Affirm a method
      • Add @before annotations before the method
    • You can declare a parameter of type jointpoint in the notification method, and then access the link details on Eng. such as the method name and the parameter value.
    • The specific code is as follows:
      • Arithmeticcalculator.java
Package Spring.aop.helloworld;public interface Arithmeticcalculator {public int add (int i, Int. j);p Ublic int sub (int i, in T j);p ublic int mul (int i, int j);p ublic int div (int i, int j);}

      • Arithmeticcalculatorimpl.java
Package Spring.aop.helloworld;import org.springframework.stereotype.Component; @Componentpublic class  Arithmeticcalculatorimpl implements Arithmeticcalculator {@Overridepublic int add (int i, int 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;}}

      • Loggingaspect.java

Package Spring.aop.helloworld;

Import Java.util.Arrays;
Import java.util.List;

Import Org.aspectj.lang.JoinPoint;
Import Org.aspectj.lang.annotation.After;
Import Org.aspectj.lang.annotation.Aspect;
Import Org.aspectj.lang.annotation.Before;
Import org.springframework.stereotype.Component;

Declare this class as a tangent: you need to put the class in an IOC container and declare it as a tangent plane
@Aspect
@Component
public class Loggingaspect {
Declares that the method is a pre-notification: Executes before the target method starts
@Before ("Execution (public int spring.aop.helloworld.arithmeticcalculator.* (int, int))")
public void Beforemethod (Joinpoint joinpoint) {
String methodName = Joinpoint.getsignature (). GetName ();
list<object> args = Arrays.aslist (Joinpoint.getargs ());
System.out.println ("the" + MethodName + "begins" + "args is:" + args);
}

Post notification: Executes after the target method has occurred (regardless of whether the method has an exception)
The sea cannot access the results of the target method execution in the post notification
@After ("Execution (public int spring.aop.helloworld.arithmeticcalculator.* (int, int))")
public void Aftermethod (Joinpoint joinpoint) {
String methodName = Joinpoint.getsignature (). GetName ();
list<object> args = Arrays.aslist (Joinpoint.getargs ());
System.out.println ("the" + MethodName + "ends" + methodName + "args is:" + args);
}
}

    • Note: There are several ways to match the Execute function, as follows:
Represents any return value execution (public * spring.aop.helloworld.arithmeticcalculator.* (int, int))//means all classes in the package execution (public * spring.aop.helloworld.*.* (int, int))//Match all public methods of the Arithmetriccalculator interface Execution (Common * arithmeticcalculator.* ( ...)) The method that matches the value returned in the Arithmetriccalculator interface for double type execute public double arithmetricclaculator.* (...) With a method with the first argument of type double, ... Match any number of arguments of any type execution public double arithmetriccalculator.* (double, ...) Method execution public double arithmetriccalculator.* the match parameter type is double, double (double)

  

Spring AOP pre-notification and post-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.