aspose file tools*
The moose likes Spring and the fly likes Annotation inside method to read property file like reading sql property file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Annotation inside method to read property file like reading sql property file" Watch "Annotation inside method to read property file like reading sql property file" New topic
Author

Annotation inside method to read property file like reading sql property file

Kr. Pallav
Ranch Hand

Joined: Mar 06, 2010
Posts: 32

Hi everyone,
I was searching if I can read any property file with annotation in Spring 3.x. I got @Value annotation which does the work almost. but there is a concern
Say I use following

Now I can use it from method and get the value of base.url.
But say if I want to read a sql property from from my DAO layer which has many queries I don't want to create that many variables ,for each query can I use anything which reads properties from method like


~|>@//@\/~
Kumar Pallav
-------------------
SCJP 5: 86 %, SCWCD: 91%
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

I am not sure I am following you. I am assuming you are using JDBC not JPA or my answer might be different.

It sounds like you want to define all of your sql in a properties file and use them in your DAO implementation. If you are using Spring 3.1.x you can register that properties file as a PropertySource and access them via the Spring environment.

For example.




This way you only have one field and you can grab your stuff off of it like a regular properties object. If you meant something else let me know. In your example it looks like you want an annotation that will magically know what property to load and store on your query field. But realistically that is not possible so I am not sure If I am understanding correctly or not.


[How To Ask Questions][Read before you PM me]
Kr. Pallav
Ranch Hand

Joined: Mar 06, 2010
Posts: 32

Hii Bill,
Pretty much what I required , Anyway Since annotations are run at compile time so I guess exactly the kind of magic I wanted will not happen but yeah, I didn't wanted to write any new codes for application to read property files and your solution works that way.

Thanks a lot Bill.
Kr. Pallav
Ranch Hand

Joined: Mar 06, 2010
Posts: 32

Bill one more thing might be silly but I am not getting the class to which Implements Environment , or Do I need to write my own implementation to use it ..
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

It is a Spring interface. The implementation is part of the framework so in my example your import for Environment should be org.springframework.core.env.environment

Java doc can be found here:
http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/core/env/Environment.html

Kr. Pallav
Ranch Hand

Joined: Mar 06, 2010
Posts: 32

imported the same and annotated with Autowire but I was getting nullpointer over it so , thought I need to define in xml for same or any component-scan to get it
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Its in a Repository or a Service class right? These classes should be themselves annotated with @Service or @Repository respectively and they should be spring managed beans if you have them included in packages picked up with a Component scan.
Kr. Pallav
Ranch Hand

Joined: Mar 06, 2010
Posts: 32

Yes Bill its under DaoImpl annotated with @Repository . If I try @value it takes Environment object is not null but when I do environment.getProperty it doesnt read it annd becomes null . Below is what I am doing
In DaoImpl


In method


in xml



Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

I'm sorry I thought your environment was null. There are 2 things that are necessary:

1. If you want the properties to be available in the environment you must register a PropertySource with the Environment
2. If you want to be able to use @Value etc you need to register a PropertySourcesPlaceholderConfigurer bean.

You have done number 2. It is important to not that if you are going to use the short hand notation



That you must use spring-context-3.1.xsd not 3.0 or else the older PropertyPlaceholderConfigurer is registered instead. I see you are doing this correctly though so that is OK.


I was eluding to #1 when I said this in my original post

If you are using Spring 3.1.x you can register that properties file as a PropertySource and access them via the Spring environment.


Using the location attribute on the context:property-placeholder does not register a PropertySource and will only be available for placeholder replacement (like @Value). The easiest way to register it as a PropertySource as well is to use the @PropertySource annotation. To do this you you can drop the location attribute making it



Now add a class to your project (I usually put in a package called config). You will want to make sure this class is in the path of your component-scan as this is what will pick it up.

Here is an example:



When this class is picked up by your component-scan it will see your @PropertySource annotation and load any property files you have defined there and register them with Environment. They will also be available to use as placeholder (like @Value).

In order to have an @Configuration class you will need to add cglib to your project dependencies.

Kr. Pallav
Ranch Hand

Joined: Mar 06, 2010
Posts: 32

Thanks a lot Bill. It's working now. Loved the learning experience with you

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annotation inside method to read property file like reading sql property file