Thursday, December 20, 2007

Guice-n' Struts2 into Warp 1

Every couple of months when I want to learn something new, I go back to the capstone project I worked on for my masters. I started with wanting to learn about Seam but quickly found that I did not like it. JBoss really does not simplify my life and having had the pleasure of being exposed to Rails, its hard for me to embrace new Java web frameworks. So I abandoned my quest to use seam and went back to seeing what else I could do.

Over the past six months I have had the pleasure of working with Spring and although I really like all of the cool things that are built in like declarative transactions, I have to admit that I am overwhelmed by all of the "copy-paste-modify" that happens inside of the XML config files. Quite frankly I am also tired of writing XML instead of Java and I really miss my days of using Pico Container. I have to also admit that I do not and never have used Spring MVC because of my love-like relationship with Struts2.

A few weeks ago I cleaning up my hard drive when I can across the Guice user manual and Fit Library 2007 and I thought to myself, "hmmm... there is some stuff I can spike". The very last section really excited me Struts2 integration! I have to admit Struts2 does suffer from the same XML problem that I have with Spring but there are some tricks to reducing the repetitive declarations, which is easy to do. So I started to put together some FIT tests using Domain Adapter and test drove some Action classes but I was still missing the declarative transactions. Thats where warp-persist comes in!

Warp-persist is an awesome Guice extension that easily hook up Hibernate with declarative transactions. Take the following example:
public class GuiceModuleConfig extends AbstractModule {

protected void configure() {
bind(Configuration.class).toInstance(new AnnotationConfiguration().configure("hibernate.cfg.xml"));
bind(CartRepository.class).to(CartRepositoryImpl.class);
install(PersistenceService.usingHibernate().across(UnitOfWork.REQUEST).buildModule());
bind(MyInitializer.class).asEagerSingleton();
}

public static class MyInitializer {
@Inject
MyInitializer(PersistenceService service) {
service.start();
}
}
}


This allows one to declare the put method of an Action as transactional!
@Transactional
public String put() {
getCart().add(bookId);
return CrudAction.PUT;
}

Easy as pie! I hope to make use of SessionPerRequestFilter so that I can start rendering lazy members of Hibernate classes. We'll see where that goes ttfn!

1 comment:

Troy Flores said...

Train your customer service people to listen and connect with customers on their terms, not yours. It will make your business grow and help you retain customers. See more capstone engineering