在Spring中使用属性文件 [英] Use properties file in Spring
问题描述
我是Spring框架的新手。在我的Spring应用程序中有一些细节,如下所示,需要在属性文件中维护这些细节。
I am a newbie to Spring framework. In my Spring application there are some details as below those details needs to be maintain in a properties file.
Transaction Name Transaction Id
TXT_CCO = 70001
TXT_CCI = 70002
TXT_SSM = 20005
In我的控制器,有一个动作如下。
In my controller, there is an action as below.
@RequestMapping(value = {"/ValidateWalletAmount**"}, method = RequestMethod.GET)
public @ResponseBody String validateWalletAmount(@RequestParam("mobile") String mobile,
@RequestParam("pin") String merchant_pin,
@RequestParam("provider") String provider,
@RequestParam("currency_type") String currency_type,
@RequestParam("amount") String amount) {
//TO DO - Get txnTypeId by provider
return "02 | 1000.00 | 0.00";
}
根据请求参数提供商我需要获取相关的交易类型ID。例如,如果提供商 TXT_CCO ,则交易类型ID应为 70001 。
According to the request parameter provider I need to get the relevant transaction type id. As an example, if the provider is TXT_CCO transaction type id should be 70001.
有人可以帮助我实现这个目标
Can someone please help me to achieve this
推荐答案
我会说你有两个选择
- 使用
< util:properties />
- 使用
@PropertySource
和环境
抽象。
加载属性
- Load the properties using
<util:properties />
- Use
@PropertySource
and theEnvironment
abstraction.
使用< util:properties />
简单加载一个属性文件,你可以使用 PropertiesFactoryBean
或更简单的< util:properties />
标签(使用 PropertiesFactoryBean
在下面,但更容易配置。)参见这里了解更多信息。
Using <util:properties />
To simply load a properties file you can use the PropertiesFactoryBean
or easier the <util:properties />
tag (which uses the PropertiesFactoryBean
underneath but is just easier to configure). See here for more information.
只需将以下内容添加到xml配置中
Simply add the following to your xml configuration
<util:properties id="transactions" location="classpath:transaction.properties" />
现在你有一个属性
bean命名为交易
您可以将其注入您的控制器,之后您可以使用它来获得您需要的房产。
Now you have a Properties
bean named transactions
which you can inject into your controller after which you can use that to obtain the property you need.
@Autowired
private Properties transactions;
使用 @PropertySource
和环境
抽象
另一个解决方案是添加一个 @Configuration
类 @PropertySource
加载属性。之后,您可以使用环境
来获取属性。请参阅 环境
部分了解更多信息。
Using @PropertySource
and Environment
abstraction
Another solution is to add a @Configuration
class with a @PropertySource
to load the properties. After that you can use the Environment
to obtain the properties. See the Environment
section in the reference guide for more information.
@Configuration
@PropertySource("classpath:transaction.properties")
public class MyConfiguration {}
在您的控制器中,您可以使用环境
来获取属性。
In your controller you can use the Environment
to obtain the properties.
@Autowired
private Environment env;
资源支持
当然Spring属性支持可用于资源加载方法。所以文件:
和 http:
前缀也可以使用,以及适用于使用过的<$的默认加载规则c $ c> ApplicationContext 。
Of course the Spring property support is usable with the Resource loading approach of Spring. So file:
and http:
prefixes work as well, as well as the default loading rules applying to the used ApplicationContext
.
这篇关于在Spring中使用属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!