We use the new environment profiles feature of spring 3.1. We currently set the active profile by setting the environment variable spring.profiles.active=xxxxx on the server to which we deploy the application.
We think this is a suboptimal solution as the war file we want to deploy should just have an additional properties file which sets the environment in which the spring app context should load so the deployment is not dependent on some env var set on the server.
I tried to figure out how to do that and found:
ConfigurableEnvironment.setActiveProfiles()
which I can use to programmatically set the profile but then I still don't know where and when to execute this code. Somewhere where the spring context loads up? Can I load the parameter I want to pass to the method from a properties file?
UPDATE: I just found at docs which I might be able to implement to set the active profile?
answer:
The answer from Thomasz is valid as long as the profile name can be provided statically in the web.xml or one uses the new XML-less configuration type where one could programmatically load the profile to set from a properties file.
As we still use the XML version I investigated further and found the following nice solution where you implement your own ApplicationContextInitializer
where you just add a new PropertySource with a properties file to the list of sources to search for environment specific configuration settings. in the example below one could set the spring.profiles.active
property in the env.properties
file.
public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static Logger LOG = LoggerFactory.getLogger(P13nApplicationContextInitializer.class);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
try {
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));
LOG.info("env.properties loaded");
} catch (IOException e) {
// it's ok if the file is not there. we will just log that info.
LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized");
}
}
}
You then need to add that initializer as a parameter to the ContextLoaderListener
of spring as follows to your web.xml
:
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>somepackage.P13nApplicationContextInitializer</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
相关推荐
springMVC+spring+ibatis 整合,包含初始化sql 为myeclipse项目,数据库配置见properites文件
log4j的配置文档。可以直接放到项目中使用。不用再到网上找配置代码
eclipse-properties文件插件 问题 在Struct2在读取语言包文件language.properties属性文件,如果文件中包含中文,会出现乱码... 如果还不能用Properites Editor打开,则文件右击 -> Open With -> PropertiesEditor即可
这是一个使用了springboot+springSecurity+jwt实现的基于token的权限管理的一个demo 具体说明可以看 使用 更改一下application.properites的数据库的一些配置信息,然后就可以运行了 首先注册的url是/auth/register ...
C# 多語言切換 語言採用XML存儲 支持 各種菜單,頁簽,ListView 樹形節點等多種控件 語言採用Resource資源存儲僅做了簡單測試
用于将CVS导入SVN库,本版本对应的JAVA版本是1.5,可放心使用。使用方法,先配置config.properites,然后使用run.bat option config-file 命令,单独执行run.bat可查看各参数作用。
首先,我们通过File->New->Maven Project进入创建Maven项目的面板 Maven项目面板,此时我们必须选中Create a simple project复选框,然后下一步: 输入项目信息,(1)首先输入域名,(2)输入项目名,(3)Web...
2、Properites->Java Compiler,右侧JDK Compilance栏,Compiler compilance level选择1.4; 二、Jdk1.5工程 1、Properties->Java Build Path->Libraries目录添加:Dm7JdbcDriver15.jar、Dm7Dialect15.jar; 2、...
Zookeeper PIDFILE 位置错误,当前导出 PIDFILE=/var/vcap/store/zookeeper/zookeeper_server.pid stardog-node 中的 slf4j 包应配置为使用 log4j 从 STARDOG_INSTALL=${JOB_DIR}/packages/stardog/s
properties 文件编辑, 可将properites中文字转换成中文
3.config.properites为工程运行配置文件,NEED_SYN_FILE_PATH:为待同步文件名列表的路径, SRC_PATH:源路径,DEST_PATH:目的路径。 4.工程运行完毕会在控制台打印运行结果,复制失败的注意检查路径 以下为运行...
SsHome_Ver2.0技术网站链接Bootstrap4 Spring文献 GitHub OAuth 2.0 OKHttp api-getUsers-demo新版本mysql修改root密码时提示语法错误的解决方法彻底解决mysql中文乱码 Json在线编辑器 Markdown编辑器项目部署说明 1...
tomcat6.0 配log4j日志的必须文件及配置过程 tomcat6-------lib | |--------tomcat-juli-... | |--------log4j.properites | |----bin |--------tomcat-juli.jar 最后把log4j.properties里的配置写好就可以了。
这是一个使用了springboot + springSecurity + jwt实现的基于令牌的权限管理的一个演示 具体说明可以看 使用 更改一下application.properites的数据库的一些配置信息,然后就可以运行了 首先注册的url是/auth/...
1、请添加built-path,对log4j-1.2.17.jar添加引用 2、请在与源文件一起放置此log4j.properites文件
例如API的MaximumValidator系统验证默认显示'${input}' is smaller than the minimum of ${minimum}. 这个API就是在properites中的key
纯Servlet+MySQL实现登录功能,页面也是Sevlet写的,下载之后修改jdbc.properites数据库用户和密码即可,内部使用了连接池功能,需要的自取,欢迎各位私聊我,
类似yahoo,google那样的动态搜索框,自己增加了上下框的功能 把DButils的那个properties的路径设置为该文件的绝对路径,然后其他的数据库配置可以到db.properites里面配置就可以了
android-14-src.rar 解压到android的sdk 的对应的版本下面,然后右键项目中的android.jar---》properites -->java source Attacherment -->external Folder --->找到刚才解压的文件夹--->ok
android-15-src.rar 解压到android的sdk 的对应的版本下面,然后右键项目中的android.jar---》properites -->java source Attacherment -->external Folder --->找到刚才解压的文件夹--->ok