Last active
February 23, 2021 09:32
-
-
Save jorgeuriarte/1093ef8f647a9d17fc4bc45bb152594e to your computer and use it in GitHub Desktop.
Bean cualificado en caliente... ejemplo de cómo tener "varios" beans del mismo tipo en el sistema, "ApplicationScoped" pero con distintas configuraciones, datasources o similares...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gailen.test.cdi; | |
import io.quarkus.redis.client.RedisClient; | |
import lombok.extern.jbosslog.JBossLog; | |
import javax.enterprise.inject.Any; | |
import javax.enterprise.inject.Instance; | |
import javax.enterprise.inject.Produces; | |
import javax.enterprise.inject.spi.InjectionPoint; | |
import javax.inject.Inject; | |
import javax.inject.Qualifier; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.ElementType.*; | |
// Defino aquí los interfaces de apoyo por mantenerlo acoplado | |
@Qualifier | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({FIELD, METHOD, TYPE}) | |
@interface DummyBeanProducer { | |
} | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) | |
@interface DummyBeanType { | |
String value(); | |
} | |
/** | |
* Aquí ya el bean "real" | |
* | |
*/ | |
@JBossLog | |
public class DummyBean { | |
/** | |
* Este objecto puede tener distintas configuraciones en función del tipo | |
* o una conexión a un datasource concreto, o una injección diferente... | |
*/ | |
private Object innerObject; | |
/** | |
* Esta factoria (@Produces & @Producer) es la que decidirá qué instancia del bean se injecta en los | |
* puntos de injección del resto de la app, en función de su anotación @DummyBeanType. | |
* | |
*/ | |
@Produces | |
@DummyBeanProducer | |
public DummyBean createSimpleBean(@Any Instance<DummyBeanProducer> instance, InjectionPoint injectionPoint) { | |
DummyBeanType type = injectionPoint.getAnnotated().getAnnotation(DummyBeanType.class); | |
log.infof("createSimpleBean %s", type.value()); | |
var dummy = new DummyBean(); | |
dummy.innerObject = type.value(); | |
return dummy; | |
} | |
public void dummy() { | |
log.infof("I'm DUMMY with inner value %s", innerObject.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gailen.test.cdi; | |
import io.quarkus.runtime.StartupEvent; | |
import lombok.extern.jbosslog.JBossLog; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.enterprise.event.Observes; | |
import javax.inject.Inject; | |
@ApplicationScoped | |
@JBossLog | |
public class AppLifecycleBean { | |
@Inject | |
@DummyBeanProducer | |
@DummyBeanType("A") | |
DummyBean beanA; | |
@Inject | |
@DummyBeanProducer | |
@DummyBeanType("B") | |
DummyBean beanB; | |
void onStart(@Observes StartupEvent ev) { | |
beanA.dummy(); | |
beanB.dummy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment