This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
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.
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.
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.
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
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.