Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute piece of java code before spring context initialization starts

Tags:

java

spring

In my application, spring config file imports are based on environment property like below

<import resource="classpath:/springcontext/text/dao_${environment}.xml" />

As we know spring configuation imports are resolved before bean (property-placeholder) creation.

But my requirment is to set the "environment" as system property from java code since environment value is not constant , it has to be set based on some logic before spring configuation imports are resolved Can I somehow assists Spring in how to archive this.

like image 975
user1283041 Avatar asked Oct 24 '25 04:10

user1283041


1 Answers

As @M. Denium rightly pointed, you can use the Interface ApplicationContextInitializer to cater this need

From Spring Docs

public interface ApplicationContextInitializer

Callback interface for initializing a Spring ConfigurableApplicationContext prior to being refreshed.

Typically used within web applications that require some programmatic initialization of the application context. For example, registering property sources or activating profiles against the context's environment. See ContextLoader and FrameworkServlet support for declaring a "contextInitializerClasses" context-param and init-param, respectively.

like image 105
Akash Yadav Avatar answered Oct 26 '25 17:10

Akash Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!