Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to access a properties file values in Spring 3?

 
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to you wire and access property file values in, say, a domain class in a Spring 3 MVC application?

If you have some validation going on with @Size or @Pattern for the domain class fields, what if you want to have the error messages that get displayed to be from a properties file instead of being hard coded.

So, I'd like to be able to have something like:



where the message above comes from a properties file.

I don't see how to either inject this information or use a properties file directly.

The Spring documentation doesn't seem to have an example for this.

Would greatly appreciate help here.

Thanks very much in advance.

mike
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if you load your property files with Spring, you can use a new annotation to inject value from property file in teh field of a Java class
Load the property file in the Spring Context with
<util:properties id="myProperties"
location="classpath:/myprops.properties" />

or
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value=":/myprops.properties" />
</bean>

Then in a Java class

@Value("#{myProperties[myPropName]}")
private String myField;

or if you have only one property file

@Value("#{myPropName}")
private String myField;

Hope that helps
 
Mike London
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ntumba lobo wrote:Well if you load your property files with Spring, you can use a new annotation to inject value from property file in teh field of a Java class
Load the property file in the Spring Context with
<util:properties id="myProperties"
location="classpath:/myprops.properties" />

or
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value=":/myprops.properties" />
</bean>

Then in a Java class

@Value("#{myProperties[myPropName]}")
private String myField;

or if you have only one property file

@Value("#{myPropName}")
private String myField;

Hope that helps



Wow, this is soooo close. THANK YOU!

However, from my code sample above, after I successfully declare the util in applicationContext.xml, if I try to do something like this:



in my domain class and then purposefully add some garbage characters.

Then, I just get the literal in the output #(errorProps[name])

That is, it's not de-referenced to show the actual message from the properties file.

The definition in the applicationContext.xml file is this:



The "name" key in the properties file should print the message: "Name is required."

Any idea what's still wrong?

Is it possible that the message tag doesn't support what I'm trying to do?

thanks again for any other suggestions.

-mike


 
ntumba lobo
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hum what if instead of

@Value("#{errorProps[name]}")
private String userNameRequired;

you try

@Value("#{errorProps.name}")
private String userNameRequired;

does it work better?
 
Mike London
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ntumba lobo wrote:hum what if instead of

@Value("#{errorProps[name]}")
private String userNameRequired;

you try

@Value("#{errorProps.name}")
private String userNameRequired;

does it work better?



------

Unfortunately, no. I still just get the same ${userNameRequired} literal output.

I appreciate all your help. Not sure what's wrong here.

Perhaps it's that I defined the application context like this?



If I used the classpath notation like you did, I always got a FileNotFoundException.

The above "full path" seems to compile OK, but it doesn't appear to be getting injected.

I get 'null' for the injected property.

How could the injection itself not be working?

Here's my entire applicationContext.xml:
 
ntumba lobo
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see so this is Spring that cant find your property file.
if you build your project with maven try putting your errors.properties under src/main/resources. After building the web app
it should end up under web-inf/lib.
In you spring config just use
<util:properties id="errorProps" location="classpath:errors.properties" />

This should work really.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic