Before CDI 2.0 register a bean dynamically is a little complex. CDI 2.0 simple the work.
publicvoidaddABean(@ObservesAfterBeanDiscovery event) {// get an instance of BeanConfiguratorevent.addBean()// set the desired data.types(Greeter.class).scope(ApplicationScoped.class).addQualifier(Default.Literal.INSTANCE)//.addQualifier(Custom.CustomLiteral.INSTANCE);//finally, add a callback to tell CDI how to instantiate this bean.produceWith(obj ->newGreeter());}
AfterBeanDiscovery event add addBean method to register a bean manually.
You can check if the bean is existed in CDI container.