• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spring 3.X has added support for expression language:
where in you could do something like:

@Value('${username}')
String someProperty;
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing this Mark
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi apurav,

Thanks for the reply.

i will try what you suggestion and will reply back.

 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Hemant. My mistake, sincere apologies! I forgot that your requirement was with propertyplaceholder.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic