user1843591
user1843591

Reputation: 1136

Dynamic CDI Injection @Named

I want to inject a @Named bean based on a configuration or the environment variable (or achieve the same goal via different means). For example i want to replace

@Inject
@Named("myredBean")

with

@Inject
@Named("{bean.color}")

or similar...

I am not using Spring, so I would prefer a non Spring solution! I am using Quarkus

Upvotes: 2

Views: 694

Answers (1)

user1843591
user1843591

Reputation: 1136

A simple way to achieve the same goal in a slightly different way is to read the environment variable when producing the CDI bean instead of when Injecting as per my question

@ConfigProperty(name = "bean.colour")
private String colour;



@Produces
public ColourBean create() {
        LOGGER.info("Producing a ColourBean for: {}",colour);



    switch(Colour.valueOf(colour)){

        case RED:
        case red:{
            return new RedBean();
       }
         ...

    }

}

Upvotes: 3

Related Questions