Moving from Spring's XML to Annotations in AppFuse
Join the DZone community and get the full member experience.
Join For Freelast night, i did a spike on appfuse to change xml to spring annotations (@repository, @service and @autowired) in its service and data modules. while i was able to accomplish everything in a few hours (including converting tests), i did run into a couple issues.
abstracttransactionaljunit4..tests vs. abstracttransactionaldatasource..tests
i've switched from
my favorite spring class
to the annotation-happy
abstracttransactionaljunit4springcontexttests
. however, this has presented an issue: when using atdssct, i was able to call
endtransaction()
and
startnewtransaction()
. with atj4sct, this doesn't seem possible. below is a screenshot of the diff on a test method in the jpa implementation of userdaotest:

on the right, you'll notice that i had to comment out
@expectedexception
to get the test to pass. this concerns me since this exception should be thrown. is there a way to call
endtransaction()
and
startnewtransaction()
when subclassing abstracttransactionaljunit4springcontexttests?
instantiating genericdao implementations programmatically
the second feature i tried to add is the ability to instantiate a genericdao programatically rather than requiring a xml bean definition. in current versions of appfuse, you can use the following bean definition to create a genericdao for a model object.
<bean id="persondao" class="org.appfuse.dao.hibernate.genericdaohibernate"> <constructor-arg value="org.appfuse.tutorial.model.person"/> <property name="sessionfactory" ref="sessionfactory"/> </bean>
when moving to a no-xml required architecture, it'd be nice to allow users to create genericdao's programmatically. below is the easiest way i've found to do this in a test:
genericdao<user, long> genericdao; @autowired sessionfactory sessionfactory; @before public void setup() { genericdao = new genericdaohibernate<user, long>(user.class); genericdao.setsessionfactory(sessionfactory); }
however, there's a couple problems with this. first of all, mixing constructor injection and setter injection probably isn't a good idea. changing the constructor to take a sessionfactory solves this problem, but now all subclasses need to have a more verbose constructor:
@autowired public userdaohibernate(sessionfactory sessionfactory) { super(user.class, sessionfactory); }
whereas before they had:
public userdaohibernate() { super(user.class); }
in an ideal world, i could call
new genericdaohibernate<user, long>(user.class)
and the sessionfactory would be wired in auto-magically. is this possible with spring 2.5?
the 2nd problem this presents is your client code will now be dependent on an implementation rather than the interface. i don't know how to solve that one, but i'd love to figure out a way to create genericdaos with no xml and no implementation details in the client. any ideas are most welcome.
if you'd like to see all the changes i made in converting from xml to annotations, please see this patch .
from http://raibledesigns.com/rd/entry/moving_from_spring_s_xml
Opinions expressed by DZone contributors are their own.
Comments