简体   繁体   English

在spring上下文初始化开始之前执行一段java代码

[英]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.

相关问题 春季4:在刷新应用程序上下文之前运行一些初始化代码 - Spring 4: run some initialization code before application context refresh 我们可以在 VertX v2 java 后端的每个 API 调用之前每次执行一个函数/一段代码吗? - Can we execute everytime a function/piece of code before each API call in VertX v2 java backend? 使用Spring,如何在初始化HIbernate之后但在应用程序启动之前执行代码? - With Spring how can I execute code after HIbernate is initialized but before the application starts? 在JMeter测试之前执行带有spring依赖项的java代码 - Execute java code with spring dependencies before JMeter test Spring ApplicationContext启动之前如何调用代码? - How to invoke code before Spring ApplicationContext starts? 如何在所有测试类开始之前执行一段代码? - How to execute a piece of code once before all test classes start? 如何确保 CompletableFuture 在执行一段代码之前完全完成? - How to ensure that CompletableFuture is completely finished before execute a piece of code? Spring 启动集成测试 - Mocking @Service 在应用程序上下文启动之前? - Spring Boot Integration Testing - Mocking @Service before Application Context Starts? 是什么导致 Spring 调度程序在应用程序和 servlet 启动之前执行? - What causes Spring scheduler to execute before the application and servlet starts up? Spring:在上下文初始化之前显示静态页面以输入数据源密码 - Spring: display static page before context initialization to enter datasource password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM