1

How to change order of callbacks in spring data jdbc? Primary, order, dependsOn used, didn't help.

@Bean
AfterDeleteCallback<? extends Document> test() {
    return (document) -> {
        log.info("test");
        return document;
    };
}

@Bean
AfterDeleteCallback<? extends Document> test1() {
    return (document) -> {
        log.info("test1");
        return document;
    };
}

Example with Order annotation:

@Bean
@Order(2)
AfterDeleteCallback<? extends Document> test() {
    return (document) -> {
        log.info("test");
        return document;
    };
}

@Bean
@Order(1)
AfterDeleteCallback<? extends Document> test1() {
    return (document) -> {
        log.info("test1");
        return document;
    };
}

logs: enter image description here

And if I do the opposite (order(1) to test method and order(2) to test1 method, the result will be the same.

@Bean
@Order(1)
AfterDeleteCallback<? extends Document> test() {
    return (document) -> {
        log.info("test");
        return document;
    };
}

@Bean
@Order(2)
AfterDeleteCallback<? extends Document> test1() {
    return (document) -> {
        log.info("test1");
        return document;
    };
}

enter image description here

sayf21
  • 11
  • 2

1 Answers1

0

Spring Data JDBC all extend the interface EntityCallback and its documentation actually describes how to order callbacks:

Multiple entity callbacks are invoked sequentially with the result of the previous callback. Callbacks are unordered by default. It is possible to define the order in which listeners for a certain domain type are to be invoked. To do so, add Spring's common @Order annotation or implement org.springframework.core.Ordered.

You mention

[..] order [..] used, didn't help

If this means you actually tried using the @Order annotation and it didn't work as intended, please post the code you used. Of course, you should also try the alternative of implementing Ordered.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Thanks for the answer! I added an example with an Order annotation to my question and logs in console. Could you take a look? – sayf21 Oct 11 '22 at 12:57