springboot应用程序:如何设置配置文件propery与spel?
问题描述:
我尝试设置有效的个人资料如下:springboot应用程序:如何设置配置文件propery与spel?
Application.properties(内部类路径)
spring.profile.active = ${app.profile}
属性 “app.profile” 来自Application.properties外JAR:
Application.properties文件(外部)
app.profile = dev
Spring拒绝加载“dev”配置文件。当我在classpath中设置“spring.profile.active”属性时,它按预期工作。
还有一个选择吗?
感谢
答
可以在web.xml
文件Web应用程序的设置活动的配置文件。请参考下面的代码片段:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev, testdb</param-value>
</context-param>
如果他们没有web.xml文件和你configuriong使用Java Web应用程序,那么你可以使用WebApplicationInitializer
。请参考下面的代码片段:
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启动应用程序时指定配置文件名称。
从CMD运行
java -jar my-first-spring-boot-app.jar -Dspring.profiles.active=dev
从Eclipse的
运行添加spring.profiles.active=dev
作为VM参数,如果你想亲语法设置轮廓
我正在与REST的应用程序只工作。 “WebApplicationInitializer”是正确的方法吗? – Idan
我的目标是用户将更改“app.profile”属性,然后spring.profile.active = $ {app.profile}将设置正确的配置文件。 所有其他外部属性均按预期工作,除配置文件外。 – Idan