Monday, April 25, 2011

@Inject inside Servlet Filter

I wanted to apply the Namespace pattern that is suggested in the Google App Engine documentation, with a CDI application (Weld implementation of JSR-299).

Since I wanted to user to choose an application during login, I needed to keep the namespace at a session bean. Since GAE is not an EE container (yet...), rather than a servlet container, I needed a different mechanizm to inject beans. I found this piece of code which came real helpful at DominikDorn.com, and created the following GaeCdiFilter abstract class:


During init(), the following abstract class get a BeanManager instance from the Servlet Context. Then it scans itself during init(), and instancing by injection each field that is annotated with @Inject.

@PostConstruct and @PreDestroy are not supported here, but it can be easily implemented as well. To use it - just extend GaeCdiFilter. If you need to override init(), don't forget to call its super (otherwise the whole thing won't work!)

No comments:

Post a Comment