0

I have a scenario where i need to show the success/error messages in jsp's from the controller. The controller has access to many methods and each method may return some message. These messages are stored as key-value pair in a property file which i need to access. Now i want to load this property file just once and use it through out the application. How can this be achieved? the framework is spring mvc. Presently i am doing something like this in every class but this approach doesn't seem right. Please help!

Properties prop = new Properties(); 
prop.load(getClass().getClassLoader().getResourceAsStream(fileName+".properties"));
Shivayan Mukherjee
  • 766
  • 3
  • 10
  • 33

2 Answers2

0

What you are looking for is probably what is described in this blog post.

In short, here is what you have to do:

First tell spring where the properties file in your spring configuration. That will look something like:

<context:property-placeholder location="classpath:application.properties"/>

Second you inject the properties into Spring beans with the @Value annotation

geoand
  • 60,071
  • 24
  • 172
  • 190
0

you can load the properties file using spring configurations as below:

<context:property-placeholder location="classpath:application.properties"/>

Add the above configuration in the spring beans configuration file and put the properties file under the source folder which are by default in the classpath.

@Value annotation can be used for injecting the property values at your controller. Something like this:

@Value("${name}")
private String name;

your properties file entries would look like this: enter code herename=your name

I hope this explanation would solve your problems.

Krishna
  • 7,154
  • 16
  • 68
  • 80