What happens first - Spring dependency injection or static block execution?

I have a class that uses a static block to initialize a static hashtable. This is done by reading the properties file, analyzing the contents of the file, and then setting the appropriate values ​​in the Hashtable.

However, instead of specifying the location of the file, I would like to enter the location instead using Spring, mainly to eliminate any hard-coded values ​​in the class. I saw somewhere else that you can actually enter a static variable, but this will be due to the use of a non-static setter.

So my question is: will the setter be called before the static block is executed, or will the static block be executed before Spring calls the installer (which will basically throw an exception in my code)?

Thank!

+5
source share
1 answer

The static initializer is executed by the class loader as part of class loading before any class is granted access to the class. Since Spring needs to create an instance of the class that definitely requires loading the class, before it can call setters on this instance, the static initializer block is already running.

+2
source

All Articles