wood burning stoves 2.0*
The moose likes Spring and the fly likes read properties in my java application defined in spring bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "read properties in my java application defined in spring bean "PropertyPlaceholderConfigurer" " Watch "read properties in my java application defined in spring bean "PropertyPlaceholderConfigurer" " New topic
Author

read properties in my java application defined in spring bean "PropertyPlaceholderConfigurer"

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi guys,

is there any way to read properties in my java application whose property file has been defined in spring bean "PropertyPlaceholderConfigurer" .

like
I have defined my property file in my xml file using spring "PropertyPlaceholderConfigurer" class


my property file has following properties


is there any way i can read this property in my java application like




apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
Spring 3.X has added support for expression language:
where in you could do something like:

@Value('${username}')
String someProperty;
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Thanks apurav,

I know that. But that is not what i am asking for.

I want to read property just like



This property belong to the file specified in spring "PropertyPlaceholderConfigurer" class.

I hope i am clear now.

Regards,
Hemant.
apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
Hmm,
Did you check the doc for PropertyPlaceholderConfigurer.
Did you tried injecting this bean in your class and see if it exposes any method to get the messages.
I am sure you could do that way.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

apurav chauhan wrote:Hmm,
Did you check the doc for PropertyPlaceholderConfigurer.
Did you tried injecting this bean in your class and see if it exposes any method to get the messages.
I am sure you could do that way.


I wouldn't take that approach. But the util namespace has a properties tag to load in a properties file as a Properties object which is a bean and can have an id that you can inject the Properties object into your class and just call myProps.getProperty("");

PropertyPlaceholderConfigurer is a BeanFactoryPostProcessor, so it has its meaning and responsibility. If you injected it into your code and used it to get values, then you just changed its meaning, which isn't a good OO practice.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
Thanks for sharing this Mark
apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
How can i forget! Its so simple!
ApplicationContext itself is a type of MessageSource.

SO just implement ApplicationContextAware and use the applicationContext object to get required message like:
applicationContext.getMessage(key)

OR

Just extend ApplicationObjectSupport. Call its getMessageSourceAccessor() method to have a reference to MessageSourceAccessor. From this object call the getMessage(key)
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi apurav,

Thanks for the reply.

i will try what you suggestion and will reply back.

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
this is what i did

my spring configuration file


This is my messageResource Class :


this is how i access MessageResource in my application


This is the error i am getting :



what i want is to read properties from PropertyPlaceholderConfigurer which i think works similar to java.util.properties and MessageSource in spring is similar to java's java.util.ResourceBundle class and completely different from PropertyPlaceholderConfigurer.


So i don't think i will be able to read property configured in PropertyPlaceholderConfigurer using messageSource(Please correct me if i am wrong).

Regards,
Hemant

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

If you are desperate to use java.util.Properties then why not just read the file in as you would do in any other non Spring application? Why complicate your Spring configuration and maintenance for something so simple?

Sean


I love this place!
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Sean,

its not that i am desperate to use java.util.properties class. its just that i don't want to hard code the same file name in 2 different location i.e on my spring config file and on my java application.





Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Well that is fine, but in that case what is wrong with Apurav Chauhan's solution? What is the reason that you don't want to use the @Value annotation?

If you can explain why you can't just inject the username and password in this way then perhaps we will be able to give you a more suitable solution...

Sean
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Sean,

The problem is we are using spring 2.5.
and even if this feature is available is 2.5, i cant go that way.

"username and password" is just the example that I have given . My requirement is that i have to read some file whose path is specified in .properties files.
The number of such path can vary from 1 to n number. key can be some static content with some incremental suffix.

for Example

location_0=path to 0 file
location_1=path to 1 file
location_2=path to 2 file
location_3=path to 3 file


So i want to read this property from my java application.

Hope i am clear with my requirement
-Hemant.

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Ok,

So do you use annotations at all?There are so many ways you could go about doing this...

1. You could set the properties file to be a MessageSource rather than using a PropertyPlaceholderConfigurer, as the name suggests you use the latter when you are wanting to use placeholders in your xml (Spring 2.5).

2. You just use a java.util.Properties instance (Would you still need the PropertyPlaceholderConfigurer?)

3. Use your placeholders to create a list bean and inject that:


and I imagine much more.

Personally I would use option 1 or 2 but it is up to you in the end...

Sean
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Sean

So do you use annotations at all
.
No we use xml based approach.

You could set the properties file to be a MessageSource rather than using a PropertyPlaceholderConfigurer, as the name suggests you use the latter when you are wanting to use placeholders in your xml (Spring 2.5).
.

yes i could use MessageSource. but it will give wrong indication because this is more of a application configuration thing and doesnt have much to do with MessageSource. anyway if i use MessageSource, i dont think i will be able to use property in my spring config file.

You just use a java.util.Properties instance (Would you still need the PropertyPlaceholderConfigurer?)


yes, but i dont want to create 2 .properties file one for spring config and other for my standalone java application .


Use your placeholders to create a list bean and inject that:


I could use that. but the number of such location varies from 1 to n.
for example. In this case i had 4 location. But it may varies from 1 to say 20 location.


-Hemant.




Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

yes, but i dont want to create 2 .properties file one for spring config and other for my standalone java application.

Why would you need 2 .properties files?

Next if you don't want to use MessageSource or java.util.Properties then I think the only option is to do something like what has been done here by making a subclass of PropertyPlaceholderConfigurer.

Sean
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Sean,

yes it will serve my purpose.
i thought may be there is some way using spring API we can do that.

any way thanks for the solution

-Hemant.
apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
You are right Hemant. My mistake, sincere apologies! I forgot that your requirement was with propertyplaceholder.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read properties in my java application defined in spring bean "PropertyPlaceholderConfigurer"