JSR-299 (CDI) configuration at runtime

Posted by nsn on Stack Overflow See other posts from Stack Overflow or by nsn
Published on 2011-11-15T14:16:47Z Indexed on 2011/11/17 9:51 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

I need to configure different @Alternatives, @Decorators and @Injectors for different runtime environments (think testing, staging and production servers).

Right now I use maven to create three wars, and the only difference between those wars are in the beans.xml files. Is there a better way to do this? I do have @Alternative @Stereotypes for the different environments, but even then I need to alter beans.xml, and they don't work for @Decorators (or do they?)

Is it somehow possible to instruct CDI to ignore the values in beans.xml and use a custom configuration source? Because then I could for example read a system property or other environment variable.

The application exclusively runs in containers that use Weld, so a weld-specific solution would be ok.

I already tried to google this but can't seem to find good search terms, and I asked the Weld-Users-Forums, but to no avail. Someone over there suggested to write my own custom extension, but I can't find any API to actually change the container configuration at runtime.

I think it would be possible to have some sort of @ApplicationScoped configuration bean and inject that into all @Decorators which could then decide themselves whether they should be active or not and then in order to configure @Alternatives write @Produces methods for every interface with multiple implementations and inject the config bean there too. But this seems to me like a lot of unnecessary work to essentially duplicate functionality already present in CDI?

© Stack Overflow or respective owner

Related posts about java

Related posts about java-ee-6