Spring:根据配置文件注入不同的属性文件 [英] Spring: Injecting different properties file according to profile

查看:276
本文介绍了Spring:根据配置文件注入不同的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,一些上下文:

我目前正在开发一个项目,在该项目中,我使用Google的AppEngine(GAE)上的Spring框架从一个项目中获取一些数据的Google服务。为此,我使用Google的OAuth工具。为此,我需要使用特定于我的应用程序的 clientSecret clientId 。由于这些是静态配置值,我使用Spring的< util:properties> 链接到文档)功能将这些值插入到
$ b XML配置:

 < util:属性id =googlePropertieslocation =WEB-INF / google.properties/> 

类用法:

  @Value(#{googleProperties ['google.data.api.client.id']})
private String clientId;

我的问题:

对于生产(当部署在App Engine上时),对于开发, clientId clientSecret 的值需要不同(在我的本地机器上)。为了解决这个问题,不需要在部署时不断改变属性文件中的值,我一直在研究Spring的配置 profiles ,这将允许我指定不同的属性文件生产和发展。虽然我有一个想法如何基于Spring配置文件工作在文档中,我完全不知道在这种情况下适当的解决方案是什么。



换句话说,我如何注入不同的属性基于我的应用程序是部署在本地还是GAE上的文件?

解决方案

几个选项:




系统变量



您可以使用前缀来控制特定于环境的属性,通过使用系统变量完成:

 < util:properties id =googleProperties
location =WEB-INF /${ENV_SYSTEM:dev}/google.properties/>

在这种情况下,它总是会显示在下面:

 < util:properties id =googleProperties
location =WEB-INF / dev / google.properties/>

默认,除非 ENV_SYSTEM code>系统变量已设置。例如,如果它设置为 qa ,它会自动查看:

 < util:properties id =googleProperties
location =WEB-INF / qa / google.properties/>






春季档案



另一种方法是使bean配置文件具体。例如:

 < beans profile =dev> 
< util:properties id =googleProperties
location =WEB-INF / google-dev.properties/>
< / beans>

< beans profile =qa>
< util:properties id =googleProperties
location =WEB-INF / google-qa.properties/>
< / beans>

合适的 googleProperties 会根据配置文件集。例如,这将加载 WEB-INF / google-dev.properties

  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); 
ctx.getEnvironment()。setActiveProfiles(dev);
ctx.load(classpath:/ org / boom / bang / config / xml / * - config.xml);
ctx.refresh();


First, some context:

I'm currently working on a project in which I use the Spring framework on Google's AppEngine (GAE) to fetch some data from one of Google's services. To do so, I make use of Google's OAuth facilities. For this, I need to use a clientSecret and clientId that are specific to my application. As these are static configuration values, I use Spring's <util:properties> (link to documentation) functionality to insert these values into the appropriate classes.

XML config:

<util:properties id="googleProperties" location="WEB-INF/google.properties" />

Class usage:

@Value("#{googleProperties['google.data.api.client.id']}")
private String clientId;

My Question:

As it turns out, the values of clientId and clientSecret need to be different for production (when deployed on App Engine) as for development (on my local machine). In order to solve this without constantly needing to change the values in the properties file when deploying, I have been looking into Spring's configuration profiles that would allow me to specify different property files for production and for development. Although I have an idea how Spring profiles work based on the documentation, I am not at all sure what the appropriate solution would be in this particular situation.

In other words, how can I inject different property files based on whether my application is deployed locally or on GAE?

解决方案

A couple of options:


System Variables

You can use a prefix to control environment specific properties, this can be done by using system variables:

 <util:properties id="googleProperties" 
                  location="WEB-INF/${ENV_SYSTEM:dev}/google.properties" />

In this case it will always look under:

 <util:properties id="googleProperties" 
                  location="WEB-INF/dev/google.properties" />

by default, unless a ENV_SYSTEM system variable is set. If it is set to qa, for example, it will automatically look under:

 <util:properties id="googleProperties" 
                  location="WEB-INF/qa/google.properties" />


Spring Profiles

Another approach is to make beans profile specific. For example:

<beans profile="dev">
    <util:properties id="googleProperties" 
                     location="WEB-INF/google-dev.properties" />
</beans>

<beans profile="qa">
    <util:properties id="googleProperties" 
                     location="WEB-INF/google-qa.properties" />
</beans>

The appropriate googleProperties will loaded depending on a profile set. For example this will load WEB-INF/google-dev.properties:

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles( "dev" );
ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
ctx.refresh();

这篇关于Spring:根据配置文件注入不同的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆