Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify intended advice execution behavior in Spring version 5.2.7+ #26202

Closed
ranjit-g opened this issue Dec 3, 2020 · 3 comments
Closed

Clarify intended advice execution behavior in Spring version 5.2.7+ #26202

ranjit-g opened this issue Dec 3, 2020 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@ranjit-g
Copy link

ranjit-g commented Dec 3, 2020

Affects: 5.2.7.RELEASE

StackOverflow question : https://stackoverflow.com/q/65032499/4214241

Sample Github project shared for the question : https://github.com/RitamChakraborty/bank_transaction_springaop/tree/r_g_answer

Issue descrption :

The following @AfterThrowing advice does not work as expected in Spring versions above : 5.2.6.RELEASE


@After("execution(public void dev.ritam.model.Bank.setTempPin(..))")
public void validatePin() {
    if (bank.getPinCode() != bank.getTempPin()) {
        throw new RuntimeException("Wrong Pin");
    } else {
        System.out.println("Correct Pin");
    }
}

@AfterThrowing(value = "execution(public void dev.ritam.model.Bank.setTempPin(..))", throwing = "e")
public void logException(Exception e) {
    System.out.println(e.getMessage());
}


For spring versions above : 5.2.6.RELEASE ( tested with releases 5.2.7.RELEASE,5.2.8.RELEASE,5.2.9.RELEASE,5.3.1)

@AfterThrowing advice does not execute on exception from @After advice. The same results if @After advice is replaced with @Before.

For spring versions equal to or below : 5.2.6.RELEASE ( tested with releases 5.2.6.RELEASE,5.2.5.RELEASE,5.2.4.RELEASE, 5.2.3.RELEASE) , the advice happens from @AfterThrowing on exception from @After

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 3, 2020
@jhoeller
Copy link
Contributor

jhoeller commented Dec 3, 2020

This seems to be a side effect of #25186 which was trying to make advice ordering more reliable. We'll try to revise this once more.

@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 3, 2020
@jhoeller jhoeller added this to the 5.3.2 milestone Dec 3, 2020
@jhoeller jhoeller added the for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x label Dec 3, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x labels Dec 3, 2020
@jhoeller jhoeller changed the title Change in advice execution behaviour from Spring version 5.2.7.RELEASE Clarify intended advice execution behavior in Spring version 5.2.7+ Dec 7, 2020
@jhoeller jhoeller self-assigned this Dec 7, 2020
@jhoeller jhoeller added type: documentation A documentation task and removed type: regression A bug that is also a regression labels Dec 7, 2020
@jhoeller
Copy link
Contributor

jhoeller commented Dec 7, 2020

It turns out that our revised advice ordering is actually correct and closely aligned with AspectJ semantics.

Each of the distinct advice types of a particular aspect is conceptually meant to apply to the join point directly. As a consequence, an @AfterThrowing advice method is not supposed to receive an exception from an accompanying @After/@AfterReturning method. This just happened to accidentally work before, in a JVM-dependent way, being at odds with AspectJ semantics.

Also, @After seems to be the wrong advice type for your purposes above. @After advice in AspectJ is defined as "after finally advice", analogous to a finally block in a try-catch statement. It will be invoked for any outcome, normal return or exception thrown from the join point (user-declared target method). In contrast to that, @AfterReturning will only apply to successful normal returns which is what your advice implementation seems to expect (otherwise your freshly thrown RuntimeException there would potentially override an original exception from the target method, silently swallowing the latter).

As a solution, keep using your @AfterThrowing method as advice for exceptions from the target method, while explicitly implementing the same logic (or delegating to the same logic) in an @AfterReturning method which contains your pin code check. This will work reliably in any AspectJ environment and in particular in any Spring version.

I've repurposed this ticket towards explicit documentation notes around the intended semantics.

jhoeller added a commit that referenced this issue Dec 7, 2020
@ranjit-g
Copy link
Author

ranjit-g commented Dec 8, 2020

Thank you for the detailed explanation.

My understanding regarding the execution order of advice types was inline with your explanation until I saw a change in behavior between versions.

My original suggestion to the SO issue was to use @Before to validate and to handle the exception with @AfterThrowing . This arrangement , to my understanding , should work. But that is not happening (I did mention this in the first post) for versions above 5.2.6.RELEASE.

Sample code

    @Before("execution(public void dev.ritam.model.Bank.setTempPin(..))")
    public void validatePin() {
        if (bank.getPinCode() != bank.getTempPin()) {
            throw new RuntimeException("Wrong Pin");
        } else {
            System.out.println("Correct Pin");
        }
    }

    @AfterThrowing(value = "execution(public void dev.ritam.model.Bank.setTempPin(..))", throwing = "e")
    public void logException(Exception e) {
        System.out.println(e.getMessage());
    }

Could you please review and confirm if that is expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants