Tuesday 7 October 2008

Adding support for @PostConstruct, @PreDestroy and @Resource to Guice

I do like guice as a Dependency Injection engine; I think it has a lot of promise; particularly if 2.0 is released soon :)

I still struggle sometimes, coming from a spring framework mindset, figuring out how to do some stuff. Though I love the @Provides support in trunk of Guice. Here's hoping 2.0 is out soon! :)

I've experimented recently adding support for ConstructorInterceptors into guice so you can add custom lifecycles like @PostContruct from JSR250/EJB3 or Spring's InitializingBean etc. 

I've also created a patch to support closing of singletons such as via @PreDestroy or spring's DisposableBean.

Slightly more wacky is a patch adding support for custom injection point annotations which allows you to easily support things like 
  • @Resource from JSR 250/EJB3
  • @PersistenceContext from JPA
  • the various JAX-RS annotations like @Context, @PathParam etc
  • WebBeans @In
  • Spring's @Autowire
  • Apache Camel's injection annotations like @EndpointInject or @Produce
Feedback greatly appreciated!


Dan Diephouse said...

Yayy!!!! I've been asking Bob for this for forever. Here's hoping it comes standard with Guice in the future and we seem some migration to it...

Unknown said...

Totally agree.
Do you think it would be possible then to have a module loaded in the camel:run instead of having to resort to spring?

James Strachan said...

Thats a great idea! I'll see what I can do...