Spring's @DynamicPropertySource registering config too late for bootstrapping Testcontainer
I am setting Spring config by running a HashiCorp Vault container for local development using Testcontainers. Before DynamicPropertySource properties are set, Spring has already configured Vault using defaults and attempts to connect to the Vault instance using these defaults (e.g. Spring configures port 8200 instead of the first mapped port of the Vault Testcontainer).
The result is a connection refused error "Connect to http://localhost:8200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: no further information". This is due to the config being incorrect and the Vault Testcontainer not running at that time.
I have a configuration class annotated @TestConfiguration and run the application main with this config as documented.
I think Spring eagerly try to connect to value to fetch properties. There are similar issues while loading properties from AWS ParameterStore/SecretsManager. One of option is to eagerly starting the Vault container using a static method/block and set Vault config properties as System Properties.
I did something similar to run Spring Cloud AWS application locally using LocalStack. Please take a look at https://youtu.be/OPQEjpX-yio?t=485