@aspect of Spring AOP

Source: Internet
Author: User

@aspect of Spring AOP

Transferred from: http://blog.csdn.net/tanghw/article/details/3862987

Starting with Spring 2.0, you can implement AOP using schema-and @aspectj-based approaches, a simple example of how to implement AOP in spring in a @aspectj way. Since @aspect is annotation-based, it is required to support more than 5.0 versions of the JDK for annotations.

Environmental requirements: 1. Web Application 2. There is a service layer dedicated to providing system services

Our goal is that if a user calls any method in the service layer, it inserts a record information into the function.

1. One of the simplest AOP

There are 2 steps.

1.1 Defining a aspect

    1.  package com.sarkuya.aop.aspect;     2.  Import Org.aspectj.lang.annotation.Aspect;     3.  import Org.aspectj.lang.annotation.Before;     4.  @Aspect     5.  public class Sampleaspect {    6. & nbsp;    @Before ("Execution (* com.sarkuya.service). *.*(..))")     7.      public void Dobeforeinservicelayer () {    8.           System.out.println ("==================================== =");     9.          System.out.println ("Aop:do before in Service layer ");     10.         System.out.println ("===================== ================");     11.  }     12. }

Line 4th, you must use @aspect to annotate before the class name.

Line 6th, when the user calls any of the methods in any of the Com.sarkuya.service packages, spring automatically executes the following Dobeforeinservicelayer () method, which simply prints some information, before the call.

1.2 Configuring in Spring configuration file Applicationcontext.xml

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:aop=" http://www.springframework.org/ Schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframewor K.org/schema/beans/spring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.O Rg/schema/aop/spring-aop-2.5.xsd ">

<aop:aspectj-autoproxy/> <bean class= "Com.sarkuya.aop.aspect.SampleAspect"/>

<!--================ YOUR CONTENTS GOES BELOW ===================-</bean>

It's so simple.

2. Separate the pointcut and advice

The above aspect are mixed with pointcut and advice, so it is best to separate them. There are 3 steps.

2.1 Definition Pointcut

1. Package com.sarkuya.aop.aspect;     2. Import Org.aspectj.lang.annotation.Aspect;     3. Import Org.aspectj.lang.annotation.Pointcut;  4. @Aspect 5.      public class Sampleaspect {6. @Pointcut ("Execution (* com.sarkuya.service).     *.*(..))")      7. public void Inservicelayer () {8.  } 9. }

Pointcut is the trigger condition for implanted advice. Each pointcut definition consists of 2 parts, one expression, such as line 6th, and a method signature, such as line 7th. The method signature must be public and void type. The method in Pointcut can be thought of as a mnemonic that is referenced by advice, because the expression is not intuitive, so we can name the expression by way of the method signature. Therefore, the method in Pointcut requires only a method signature, rather than writing the actual code in the method body.

2.2 Definition Advice

    1.  package com.sarkuya.aop.advice;     2.  Import Org.aspectj.lang.annotation.Aspect;     3.  import Org.aspectj.lang.annotation.Before;     4.  @Aspect     5.  public class Sampleadvice {    6. & nbsp;    @Before ("Com.sarkuya.aop.aspect.SampleAspect.inServiceLayer ()")     7.       public void Loginfo () {    8.           System.out.println ("=====================================");     9.          System.out.println ("Aop:do before in Service layer ");     10.         System.out.println ("===================== ================");     11.    }     12. }

Line 4th, for advice, you can only use @aspect to annotate.

Line 6th, unlike the 6th line in section 1.1, is not an expression that uses pointcut directly, but rather uses the method signature in Pointcut.

The benefit of defining pointcut alone is that it makes the code more intuitive by using meaningful method names rather than hard-to-read pointcut expressions, and that pointcut can be shared and called directly by multiple advice. If more than one advice calls a pointcut, and the pointcut expression changes in the future, only one place can be changed, and maintenance is more convenient.

In line 7th, we change the advice method to Loginfo () to clarify the role of this advice.

2.3 Configuration Files

<aop:aspectj-autoproxy/> <bean class= "Com.sarkuya.aop.advice.SampleAdvice"/>

Simply configure the sampleadvice without configuring the Sampleaspect.

3. Refactoring: Clear Pointcut responsibilities

For Sampleaspect, its primary responsibility is to define pointcut, which can define multiple pointcuts at the same time in this class. But its class name does not reflect this feature, so it should be refactored to clarify its responsibilities.

Package com.sarkuya.aop.pointcut;     Import Org.aspectj.lang.annotation.Aspect;     Import Org.aspectj.lang.annotation.Pointcut; @Aspect public class Pointcutsdefinition {@Pointcut ("Execution (* com.sarkuya.service).         *.*(..))") public void Inservicelayer () {}}

Rename the Sampleaspect to Pointcutsdefinition and move to the Com.sarkuya.aop.pointcut package.

For Sampleadvice, just change the annotation for @Before (), point to @Before ("Com.sarkuya.aop.pointcut.PointcutsDefinition.inServiceLayer ()")

The spring configuration file remains unchanged.

Summary: We start with a simple aspect instance, understand the role of AOP and the most basic requirements, and then refactor into more meaningful code, the concept of pointcut and advice in AOP, helps us to build more complex aspect.

Spring AOP's @aspect

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.