#To PostConstruct or not to PostConstruct, that is thy question.

1 messages · Page 1 of 1 (latest)

blissful estuary
#

Spring Java question: So I ran into a situation that I struggled with the other day, and I was hoping to seek some council to see what good patterns are. I started down the path of "chaining" postconstruct beans together, and eventually came to the realization that maybe spending the last 2-3 hours of fighting the framework and order of beans being instantiated into the Spring context highlights i'm doing something wrong. My specific case was wiring up a bean that created a table structure (sort of like what liquibase does, but I wrote this using manual JDBC interactions upon app instantiation to feel a little pain). Is there a better way to build in these types of dependencies than postconstruct that I might just be missing that would facilitate data insertion upon table creation in this bean?

worldly cosmosBOT
#

<@&1004656351647117403> please have a look, thanks.

worldly cosmosBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

blissful estuary
#

Constructor injection I suppose makes sense, but configuration beans are singletons such that I'd want one bean that creates all the tables and subsequent beans that load the data after the table structure.

shadow cave
#

you can use an ApplicationReadyEvent listener

#

it gets invoked as late as possible during startup

#

so everything should be up and eunning at that point

blissful estuary
#

Thanks for the response Nem! Requests could be hitting my controller though if the application is up, and if the data is missing from the cached database layer the api response could provide inconsistent results in that very small window. Maybe I'm missing something though?

shadow cave
#

yea could be. do you use kubernetes?

#

also whats the problem with doing constructor injection and running the initialization in the constructor

blissful estuary
#

I think maybe I was thinking about this wrong, and you asking the question again (serving as my rubber duck) made me think harder. I was operating under the assumption that constructor injecting the same config bean twice creates two seperate beans, when in fact that's completely wrong (right?) so technically I guess I could have just done that I guess.

shadow cave
#

yea usually beans are singletons

blissful estuary
#

hmm I might try a refactor tonight and see if this works. Thanks Nem!

shadow cave
#

good luck 🙂

worldly cosmosBOT
#

Closed the thread.

blissful estuary
#

weird...spacing

strong oasis
blissful estuary
#

No, and I thought I closed the thread properly. Sorry

cinder wren