Monday, 26 March 2012

How to get value from properties file in java class using spring?

To get property key-value from .properties file,

1) Declare <context:property-placeholder> tag in spring xml.
2) Declare a variable with required type and annotate the variable with @value.

 Ex:
env.properties
--------------

user.name=prabha
user.has.phone=true

in java class
------------

@value("${useer.name}")
private String name;

@value("${user.has.phone}")
private boolean hasPhone;

//setters and getters

No comments:

Post a Comment