Tuesday, August 14, 2012

Using @Configurable to inject resources into objects created using "new" operator (domain objects)

I am currently developing a query language based on a context-free grammar. The library contains a parser that parses the queries accepted by the language and returns a tree of parse entries. So, these parse entries essentially form the domain objects of the application. The library also provides the evaluator framework to evaluate the state of an object against a parse tree. A parse entry contains a reference to the evaluator that should be used to evaluate the state of an object against itself.
Since the parse entries are created using the new operator during the parse process and since it is not a good idea to hard-wire the evaluators into the parse entry classes, I was looking for a way to inject them. Then I came across the @Configurable annotation provided by Spring framework to inject beans into objects of a class whose objects are instantiated using the new operator instead of getting them from the Spring framework. I followed the Load Time Weaving approach to get this working, in a sample project, as follows:

The setter method for accountDAO must be public for Spring to find it and inject it(I found this strange as I thought the reflection based frameworks can access protected methods as well, which is the case with Hibernate.)

Sir what does this mean: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:META-INF/application-config.xml" }) and Run the test class with the JVM argument -javaagent:/path/to/org.springframework.instrument.jarhow can i do the 7th step... please am begging you to explain me sir.. please please .. my mail id is: nakirekantisaikumar@gmail.com