I've been trying to implement @PostConstruct
and @PreDestroy
methods in an Account
class of mine. Neither of them worked in the following situation, but for the sake of brevity, I'll only talk about @PostConstruct
.
I'm using Spring Batch's readers to load these accounts from a fixed-length file. So far, so good, except when my reader creates these accounts, it apparently does not call the @PostConstruct
method (debug breakpoints are never activated, and log messages are not printed out).
The reader is only custom in the sense that it's a custom class extending FlatFileItemReader<Account>
and setting values in the constructor.
Adding the exact same initialization method (that never got called in the Account
class) to the reader itself works just fine.
I.e. if the @PostConstruct
method should be called upon reader initialization, it works. Just not when the reader itself initializes accounts annotated with @PostConstruct
.
If I add a breakpoint or log message in the constructor of Account
directly, it also works without an issue.
Is this desired behaviour by Spring Batch? Or could this be caused by any configuration of mine?
Another question's answer mentioned that annotations such as @PostConstruct
"only apply to container-managed beans", not if "you are simply calling new BlogEntryDao()
yourself".
Is this what's happening here - Spring Batch calling new Account(...)
directly, without registering them in the container? After all, I never have these accounts available as beans or anything.