user1283041
user1283041

Reputation: 63

Execute piece of java code before spring context initialization starts

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.

Upvotes: 4

Views: 1197

Answers (1)

Akash Yadav
Akash Yadav

Reputation: 2421

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.

Upvotes: 3

Related Questions