Java springboot应用程序:如何使用spel设置配置文件属性?

Java springboot应用程序:如何使用spel设置配置文件属性?,java,spring,spring-boot,Java,Spring,Spring Boot,我尝试按如下方式设置活动配置文件: Application.properties(类路径内部) 属性“app.profile”来自JAR外部的Application.properties: Application.properties文件(外部) Spring拒绝加载“dev”配置文件。当我在类路径中设置“spring.profile.active”属性时,它会按预期工作 还有别的选择吗 谢谢您可以在web应用程序的web.xml文件中设置活动配置文件。请参阅下面的代码片段: <conte

我尝试按如下方式设置活动配置文件:

Application.properties(类路径内部)

属性“app.profile”来自JAR外部的Application.properties:

Application.properties文件(外部)

Spring拒绝加载“dev”配置文件。当我在类路径中设置“spring.profile.active”属性时,它会按预期工作

还有别的选择吗


谢谢

您可以在web应用程序的
web.xml
文件中设置活动配置文件。请参阅下面的代码片段:

<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>dev, testdb</param-value>
</context-param>
class WebAppInitializer extends WebApplicationInitializer {

  void onStartup(ServletContext container) {
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.getEnvironment().setActiveProfiles("profileName");
      rootContext.register(SpringConfiguration.class);
      container.addListener(new ContextLoaderListener(rootContext));
  }
}
WebAppInitializer
需要使用
@Configuration
进行注释,才能正常工作


如果有帮助或需要更多帮助,请告诉我。

您可以在启动Spring boot应用程序时指定配置文件名称

从CMD运行

java -jar my-first-spring-boot-app.jar -Dspring.profiles.active=dev
从Eclipse运行
添加
spring.profiles.active=dev
作为VM参数

如果您想按专业语法设置配置文件,请参考以下url

我正在使用仅限REST的应用程序。“WebApplicationInitializer”是正确的方法吗?我的目标是用户将更改“app.profile”属性,然后spring.profile.active=${app.profile}将设置正确的配置文件。除配置文件外,所有其他外部属性均按预期工作。是否可以更改属性名称?原因用户将编辑这些外部属性没有属性不能更改(spring.profiles.active)只有值(dev)可更改我的目标是用户将更改“app.profile”属性,然后spring.profile.active=${app.profile}将设置正确的配置文件。除配置文件外,所有其他外部属性均按预期工作
class WebAppInitializer extends WebApplicationInitializer {

  void onStartup(ServletContext container) {
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.getEnvironment().setActiveProfiles("profileName");
      rootContext.register(SpringConfiguration.class);
      container.addListener(new ContextLoaderListener(rootContext));
  }
}
java -jar my-first-spring-boot-app.jar -Dspring.profiles.active=dev