DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Moving from Spring's XML to Annotations in AppFuse

Moving from Spring's XML to Annotations in AppFuse

Matt Raible user avatar by
Matt Raible
·
Nov. 05, 08 · Interview
Like (0)
Save
Tweet
Share
16.07K Views

Join the DZone community and get the full member experience.

Join For Free

last 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:

abstracttransactionaljunit4springcontexttests vs. abstracttransactionaldatasourcespringcontexttests

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

XML Annotation AppFuse

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fargate vs. Lambda: The Battle of the Future
  • HTTP vs Messaging for Microservices Communications
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • The Path From APIs to Containers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: