{"record":{"id":"a95ff89bff4292a9","repo":"flowable/flowable-engine","slug":"cdi-beanmanager-cannot-find-an-instance-of-request","errorCode":null,"errorMessage":"CDI BeanManager cannot find an instance of requested type ${clazz.getName()}","messagePattern":"CDI BeanManager cannot find an instance of requested type (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/util/ProgrammaticBeanLookup.java","lineNumber":34,"sourceCode":"import java.util.Iterator;\r\nimport java.util.Set;\r\n\r\nimport jakarta.enterprise.context.spi.CreationalContext;\r\nimport jakarta.enterprise.inject.spi.Bean;\r\nimport jakarta.enterprise.inject.spi.BeanManager;\r\n\r\n/**\r\n * Utility class for performing programmatic bean lookups.\r\n * \r\n * @author Daniel Meyer\r\n */\r\npublic class ProgrammaticBeanLookup {\r\n\r\n    @SuppressWarnings(\"unchecked\")\r\n    public static <T> T lookup(Class<T> clazz, BeanManager bm) {\r\n        Iterator<Bean<?>> iter = bm.getBeans(clazz).iterator();\r\n        if (!iter.hasNext()) {\r\n            throw new IllegalStateException(\"CDI BeanManager cannot find an instance of requested type \" + clazz.getName());\r\n        }\r\n        Bean<T> bean = (Bean<T>) iter.next();\r\n        CreationalContext<T> ctx = bm.createCreationalContext(bean);\r\n        T dao = (T) bm.getReference(bean, clazz, ctx);\r\n        return dao;\r\n    }\r\n\r\n    @SuppressWarnings({ \"unchecked\", \"rawtypes\" })\r\n    public static Object lookup(String name, BeanManager bm) {\r\n        Set<Bean<?>> beans = bm.getBeans(name);\r\n        if (beans.isEmpty()) {\r\n            throw new IllegalStateException(\"CDI BeanManager cannot find an instance of requested type '\" + name + \"'\");\r\n        }\r\n        Bean bean = bm.resolve(beans);\r\n        CreationalContext ctx = bm.createCreationalContext(bean);\r\n        // select one beantype randomly. A bean has a non-empty set of\r\n        // beantypes.\r\n        Type type = (Type) bean.getTypes().iterator().next();\r","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/util/ProgrammaticBeanLookup.java#L16-L52","documentation":"ProgrammaticBeanLookup.lookup(Class<T>, BeanManager) throws IllegalStateException when bm.getBeans(clazz) returns no bean of the requested type — the CDI container has no eligible bean matching the class. This happens during programmatic lookup of engine services (e.g. Manager instance lookup) when the required bean is not in the deployed CDI archive.","triggerScenarios":"Calling ProgrammaticBeanLookup.lookup(SomeClass.class, beanManager) where no bean with that type exists — missing @Injectable/producer, class not annotated as a bean, or the class isn't in a bean archive discovered by CDI.","commonSituations":" beans.xml/bean-discovery-mode excludes the package; flowable-cdi beans not on the classpath; missing producer method for an interface type; lookup by concrete class when only an interface is exposed.","solutions":["Ensure the requested type is an available CDI bean (add a @Produces or a managed bean definition for it).","Include the package in bean discovery (beans.xml bean-discovery-mode=\"all\" or annotated with proper scope annotations).","Check classpath: the flowable-cdi module's beans must be in a bean archive visible to the CDI container.","Lookup by a type that is actually registered (e.g. the interface with a producer) instead of the concrete class."],"exampleFix":"// before\nMyService s = ProgrammaticBeanLookup.lookup(MyService.class, bm); // no bean\n// after: define a producer\n@Produces public MyService produce() { return new MyServiceImpl(); }","handlingStrategy":"validation","validationCode":"if (bm.getBeans(MyService.class).isEmpty()) {\n    throw new IllegalStateException(\"Register a CDI bean/producer for MyService before lookup\");\n}\nMyService s = ProgrammaticBeanLookup.lookup(MyService.class, bm);","typeGuard":"boolean beanOfTypeExists(Class<?> clazz, BeanManager bm) {\n    return !bm.getBeans(clazz).isEmpty();\n}","tryCatchPattern":"try {\n    s = ProgrammaticBeanLookup.lookup(MyService.class, bm);\n} catch (IllegalStateException e) {\n    s = new MyServiceImpl(); // local default instead of CDI-managed\n}","preventionTips":["Verify the target type has a scope annotation or @Produces before programmatic lookup.","Keep flowable-cdi packages inside a discovered bean archive (beans.xml / discovery mode).","Lookup interfaces that actually have producers rather than concrete classes.","Add a container bootstrap test that resolves all programmatically looked-up beans."],"tags":["flowable","cdi","bean-lookup"],"backgroundTag":"resource-not-found","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}