Annotation-based Spring AOP: @ afterreturning

Source: Internet
Author: User

AfterreturningEnhanced processing willAfter the target method is properly completed, it is woven.

Use @ afterreturning to specify the following two attributes:

① Pointcut/value: both are used to specify the cut-in expression corresponding to the start point.

Returning: Specify a return value parameter name. The method for enhancing the processing definition can access the return value of the target method through the parameter name of this row.

Person. Java:

 
Public interface person {Public String sayhello (string name); Public void eat (string food );}

Chinese. Java:

Import Org. springframework. stereotype. component; @ componentpublic class Chinese implements person {@ overridepublic void eat (string food) {system. out. println ("I'm eating:" + food) ;}@ overridepublic string sayhello (string name) {system. out. println ("sayhello method executed"); Return name + "Hello, Spring AOP ";}}

Afterreturningadvicetest. Java:

 
Import Org. aspectj. lang. annotation. afterreturning; import Org. aspectj. lang. annotation. aspect;/*** define a plane ** @ author administrator **/@ aspectpublic class afterreturningadvicetest {@ afterreturning (returning = "RVT", pointcut = "execution (* COM. bean. *. *(..)) ") Public void log (Object RVt) {system. out. println ("Return Value of the target method:" + RVt); system. out. println ("log simulation function... ");}}

Bean. 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: context = "http://www.springframework.org/schema/context" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context: component-scan base-package = "com. bean "> <context: Include-filter type =" annotation "expression =" org. aspectj. lang. annotation. aspect "/> </Context: component-scan> <AOP: aspectj-autoproxy/> </beans>

Test. Java:

 
Public class test {public static void main (string [] ARGs) {applicationcontext CTX = new classpathxmlapplicationcontext ("bean. XML "); person P = (person) CTX. getbean ("Chinese"); system. out. println (P. sayhello ("James"); p. eat ("watermelon ");}}

Running console output:

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.