dengshuo
dengshuo

Reputation: 59

I want multiple @ postconstruct methods in a class to be executed sequentially

I hope they will be executed in the specified order, but the execution result is random,Use @order invalid,Each method is independent, temporarily unable to put it in one method

Upvotes: 0

Views: 740

Answers (3)

thinhhja2001
thinhhja2001

Reputation: 11

Try this

public void method1() {
        System.out.println("method 1");
    }

    public void method2() {
        System.out.println("method 2");
    }

    public void method3() {
        System.out.println("method 3");
    }

    @PostConstruct
    public void postConstruct() {
        method1();
        method2();
        method3();
    }

result:

method 1
method 2
method 3

Upvotes: 0

dengshuo
dengshuo

Reputation: 59

@Bean
public void test1(){
    System.out.println("bean A init");
}


@Bean
@DependsOn("test1")
public void test2(){
    System.out.println("test2");
}


@Bean
@DependsOn("test2")
public void test3(){
    System.out.println("test3");
}

You can use this scenario to control the loading order

Upvotes: 1

pg26
pg26

Reputation: 54

You can try calling these methods from a single @postconstruct method.

Upvotes: 2

Related Questions