aspose file tools*
The moose likes Spring and the fly likes How to access multiple property files using the Spring 3.1+ Environmnent Abstraction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to access multiple property files using the Spring 3.1+ Environmnent Abstraction" Watch "How to access multiple property files using the Spring 3.1+ Environmnent Abstraction" New topic
Author

How to access multiple property files using the Spring 3.1+ Environmnent Abstraction

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Hello,

The code snippet below works fine with the other Environmental Abstraction features to let me access the "app.properites" file's values.

My question is what if I have another properties file I also want to include?

If I try to add another @PropertySource, it is regarded as a duplicate and the class will no longer compile.

Do I need to create an entirely new class for each @PropertySource (property file) I want to include or is there some other trick?

Thanks,

mike

----------------

Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Hi Mike,

If you are using spring 4.0, you can use this annotation or if you are using spring 3.0+, you can use either mention the configuration in config file like below or call the setLocations method in your class directly. In the latter case, you will not require to use the PropertySource annotation.
Hope this helps.

Thanks,
Manish

Manish S.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Manish Sridharan wrote:Hi Mike,

If you are using spring 4.0, you can use this annotation or if you are using spring 3.0+, you can use either mention the configuration in config file like below or call the setLocations method in your class directly. In the latter case, you will not require to use the PropertySource annotation.
Hope this helps.

Thanks,
Manish



Thanks for your reply.

Since we're using Spring 3.2.5 (not 4), it appears we need to use the setLocations as you indicated.

Do you have a quick example how to do that (on what instance?)?

thanks,

- mike
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
This method is defined on the PropertyPlaceholderConfigurer. For a more detailed example, you should check my BFF - Google. He will give you more details on this.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Manish Sridharan wrote:This method is defined on the PropertyPlaceholderConfigurer. For a more detailed example, you should check my BFF - Google. He will give you more details on this.


Sorry, I was posting my reply last night on the go.

Found setLocation as a method in "PropertySourcesPlaceholderConfigurer" too.

Experimenting with multiple:

p.setLocation(new ClassPathResource("classpath:/app.properties"));

But might need to create an array and use p.setLocations

thanks,

mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Using the setLocations, it does not find the property files in the classpath the way the @PropertySource("classpath:/app.properties") annotation did in the same class.

Using the approach below, I now get nulls for the Java code that references the properties.

I was using the approach shown here: http://www.javacodegeeks.com/2012/02/properties-with-spring.html?ModPagespeed=noscript

I also tried "/app.properties" and "classpath:/app.properties" in the new ClassPathResource, but these did not work either.

Look forward to any ideas why this approach isn't working.

Thanks,

mike

-------

[code=java][
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{

PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =
new PropertySourcesPlaceholderConfigurer();

Resource[] resources = new ClassPathResource[]
{new ClassPathResource("app.properties")};

propertySourcesPlaceholderConfigurer.setLocations(resources);
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);

return propertySourcesPlaceholderConfigurer;
/code]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7



Multiple of same annotation per class will not be possible until java 8
http://download.java.net/jdk8/docs/api/java/lang/annotation/Repeatable.html


[How To Ask Questions][Read before you PM me]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Mike London wrote:

I also tried "/app.properties" and "classpath:/app.properties" in the new ClassPathResource, but these did not work either.

Look forward to any ideas why this approach isn't working.


try new ClassPathResource("app.properties")

Although you can define multiple on the annotation as I just mentioned.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Bill Gorder wrote:
Mike London wrote:

I also tried "/app.properties" and "classpath:/app.properties" in the new ClassPathResource, but these did not work either.

Look forward to any ideas why this approach isn't working.


try new ClassPathResource("app.properties")

Although you can define multiple on the annotation as I just mentioned.


Hi Bill,

You're amazing.

The multiple property approach in the annotation works fine.

Your other suggestion about how to define the ClassPathResource was how I was doing it and that was not working. Strange. I also tried using "file:" prefix and getting the reference path from the IDE, but that did not work either.

If you have any other ideas on why that code (above) wouldn't work, I'd like to hear them. Otherwise, your "rabbit out of the hat" about multiple property files in the annotation works perfectly!

Thanks very much Bill.

mike
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Mike,

Your welcome.

new ClassPathResource(app.properties) should work. If it does not you will get a file not found exception. The problem with loading them this way is that the properties files loaded in this fashion are only available for property replacement (much like the older PropertyPlaceholderConfigurer). Since it is not registered as a PropertySource it is not available through the environment abstraction. In other words if you load it this way you can do this



However you cannot do this:

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Bill Gorder wrote:Mike,

Your welcome.

new ClassPathResource(app.properties) should work. If it does not you will get a file not found exception. The problem with loading them this way is that the properties files loaded in this fashion are only available for property replacement (much like the older PropertyPlaceholderConfigurer). Since it is not registered as a PropertySource it is not available through the environment abstraction. In other words if you load it this way you can do this



However you cannot do this:



Bill,

All of this works perfectly as usual. Thanks for the clarification above. That was exactly the problem I was having.

How do you know this stuff so completely?

I cannot find any printed or web resource that even comes close, seriously. Hopefully when the new Spring books come out, there will be examples that approach the excellent support you have already given me here.

Did you write Spring?

Thanks very much as always!

-mike
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Your very welcome. I have been using Spring for enterprise development for a while now I have also used it to create domain specific framework extensions. In the process I have gotten very familiar with the internal workings, and less documented features. I would love to write a blog or a book, but it is always hard to find the time.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Bill Gorder wrote:Your very welcome. I have been using Spring for enterprise development for a while now I have also used it to create domain specific framework extensions. In the process I have gotten very familiar with the internal workings, and less documented features. I would love to write a blog or a book, but it is always hard to find the time.


Well, please let me know if you do any of these, or teach a class.

Thanks again.

mike
 
Don't get me started about those stupid light bulbs.
 
subject: How to access multiple property files using the Spring 3.1+ Environmnent Abstraction