This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes How to access a properties file values in Spring 3? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to access a properties file values in Spring 3?" Watch "How to access a properties file values in Spring 3?" New topic
Author

How to access a properties file values in Spring 3?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
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
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
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


SCJP 5 , SCWCD 5, SCEA 5
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
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

Joined: Oct 21, 2008
Posts: 180
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
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

Joined: Oct 21, 2008
Posts: 180
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.

 
wood burning stoves
 
subject: How to access a properties file values in Spring 3?
 
Similar Threads
Setting properties in my APP using Spring
This weeks giveaway
properties file using the ResourceBundle class?
.properties in the jsp
Can I use Spring 3 message resource bundle with another framework?