[cdi-dev] Interceptors for qualified @PostConstruct methods?

Craig Ringer craig at postnewspapers.com.au
Tue Aug 2 01:31:24 EDT 2011


Hi all

I've been running into a few cases where it'd be rather handy to be able
to add handling of @PostConstruct (and to a lesser extent @PreDestroy)
to a CDI interceptor, so I can run code before/after a @PostConstruct
method is invoked.

My specific use case is PrettyFaces, where I'd like to be able to
perform additional injection into a class before @PostConstruct is run.
URL query parameters are to be injected when a bean is constructed,
before @PostConstruct is called. PrettyFaces currently uses an action
method that's run at every request instead, but this works poorly for
beans that are of scopes other than @RequestScoped and requires some
hacks to use effectively.

It's possible that this particular use case can be solved using a
portable extension. PrettyFaces could add new Bean instances with a
prettyfaces-specific qualifier, then app classes can use that qualifier
on injection sites. Parameters not passed may be injected as null; the
injected objects would be of dependent scope so that should be OK. I'm
not sure that'll work yet, though, and I thought it'd be worth raising
the idea of intercepting @PostConstruct and @PreDestroy anyway, in case
that sets off ideas for others.

Opinions? Is this a reasonable thing to want? Any obvious pitfalls or
downsides to adding support for it?

--
Craig Ringer


More information about the cdi-dev mailing list