[英]Execute piece of java code before spring context initialization starts
In my application, spring config file imports are based on environment property like below 在我的应用程序中,spring配置文件导入基于环境属性,如下所示
<import resource="classpath:/springcontext/text/dao_${environment}.xml" />
As we know spring configuation imports are resolved before bean (property-placeholder) creation. 我们知道在创建bean(property-placeholder)之前解析spring配置导入。
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. 但我的要求是将“环境”设置为来自java代码的系统属性,因为环境值不是常量,必须在解决Spring配置导入之前根据某些逻辑设置我能以某种方式协助Spring如何归档这个。
As @M. 作为@M。 Denium rightly pointed, you can use the Interface ApplicationContextInitializer to cater this need Denium正确指出,您可以使用Interface ApplicationContextInitializer来满足这种需求
From Spring Docs 来自Spring Docs
public interface ApplicationContextInitializer 公共接口ApplicationContextInitializer
Callback interface for initializing a Spring ConfigurableApplicationContext prior to being refreshed. 回调接口,用于在刷新之前初始化Spring ConfigurableApplicationContext。
Typically used within web applications that require some programmatic initialization of the application context. 通常在需要对应用程序上下文进行某些编程初始化的Web应用程序中使用。 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. 请参阅ContextLoader和FrameworkServlet支持,分别声明“contextInitializerClasses”context-param和init-param。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.