I have created a spring framework based application using AnnotationConfigApplicationContext
.
One bean has an init method which creates a connection to an external service. This can be annotated with @PostConstruct
to run automatically once the bean is initiated which works.
To handle any exceptions when creating this connection I want my init method to retry up to 5 times if an exception is caught. When annotating the method with both @PostConstruct
and @Retryable
I see the exception is thrown once and the program exits- It appears @Retryable
has no effect.
I have used @EnableRetry
in the configuration class correctly along with @Configuration
. I have created another method B on the same bean which is annotated as retryable, if this method is called after the bean is instantiated I can see the method gets retried/behaves as expected when an exception is thrown.
My thoughts as to why this is not working is possibly something aspect related or the the postconstruct happens before the spring-retry element gets attached?
Is there actually a better way to have an initialization method that can handle exceptions and be retryable via annotations- instead of trying programmatically in the method?
Edit: I agree now that creating connections to external services should not be done via @Postconstruct
. This can stop the whole context from initialising if the retries fail which can have detrimental effects.
However this does not yet answer the question of what part of the Spring Framework is not letting these two annotations work in conjunction.