0

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 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";
}

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

Rose18
  • 2,892
  • 8
  • 47
  • 98
  • 1
    What did you try? what issue are you facing? *as is* this is too broad –  Aug 25 '15 at 04:45

2 Answers2

2

I would say you have 2 options

  1. Load the properties using <util:properties />
  2. Use @PropertySource and the Environment abstraction.

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.

Simply add the following to your xml configuration

<util:properties id="transactions" location="classpath:transaction.properties" />

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;

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;

Resource Support

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
0

Lets say your properties file name is "messages.properties", in that case you need to have something similar to following in your applicationContext.xml file.

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

Make sure that your properties file is in the class path.

In your controller class, auto-wire the messagesource object

    @Autowired
    private MessageSource messageSource;

To compare the transaction type id you can have code similar to below:

   if(provider.trim().equals("TXT_CCO")){
        String transactionTypeId=messageSource.getMessage("TXT_CCO", null, Locale.ENGLISH).trim();
    }
Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
  • @Ann Basically you need to inject the property file values as a Map object in your controller class. You can also have a look at this answer: http://stackoverflow.com/a/21280982/1898397 – Tarun Gupta Aug 25 '15 at 05:05
  • That isn't the right way, that is for message resolving and I18N support. For simply loading a properties file use `` and simply inject the resulting `Properties` object in your controller. – M. Deinum Aug 25 '15 at 05:50