简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM