{"record":{"id":"10c67a6ce08166a6","repo":"hibernate/hibernate-orm","slug":"unable-to-instantiate-specified-event-listener-cla","errorCode":null,"errorMessage":"Unable to instantiate specified event listener class: ","messagePattern":"Unable to instantiate specified event listener class: ","errorType":"exception","errorClass":"EventListenerRegistrationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerRegistryImpl.java","lineNumber":155,"sourceCode":"\tprivate <T> @Nonnull T resolveListenerInstance(@Nonnull Class<T> listenerClass) {\n\t\tfinal T listenerInstance = listenerClass.cast( listenerClassToInstanceMap.get( listenerClass ) );\n\t\tif ( listenerInstance == null ) {\n\t\t\tfinal T newListenerInstance = instantiateListener( listenerClass );\n\t\t\tlistenerClassToInstanceMap.put( listenerClass, newListenerInstance );\n\t\t\treturn newListenerInstance;\n\t\t}\n\t\telse {\n\t\t\treturn listenerInstance;\n\t\t}\n\t}\n\n\tprivate <T> @Nonnull T instantiateListener(@Nonnull Class<T> listenerClass) {\n\t\ttry {\n\t\t\t//noinspection deprecation\n\t\t\treturn listenerClass.newInstance();\n\t\t}\n\t\tcatch ( Exception e ) {\n\t\t\tthrow new EventListenerRegistrationException(\n\t\t\t\t\t\"Unable to instantiate specified event listener class: \" + listenerClass.getName(),\n\t\t\t\t\te\n\t\t\t);\n\t\t}\n\t}\n\n\t@Override\n\t@SafeVarargs\n\tpublic final <T> void setListeners(@Nonnull EventType<T> type, @Nullable T... listeners) {\n\t\tfinal var registeredListeners = getEventListenerGroup( type );\n\t\tregisteredListeners.clear();\n\t\tif ( listeners != null ) {\n\t\t\tfor ( T listener : listeners ) {\n\t\t\t\tregisteredListeners.appendListener( listener );\n\t\t\t}\n\t\t}\n\t}\n","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerRegistryImpl.java#L137-L173","documentation":"EventListenerRegistrationException wrapping the failure of Class.newInstance() on an event listener class, thrown from EventListenerRegistryImpl.instantiateListener. It is used by the registry's class-based registration paths (e.g. setListenerClasses / listener classes declared in configuration); the original reflective error (NoSuchMethodException, IllegalAccessException, or a constructor exception) is attached as the cause. The message names the listener class that could not be built.","triggerScenarios":"Declaring an event listener as a class name in hibernate.cfg.xml <event> blocks or calling registry.setListenerClasses(type, MyListener.class) where MyListener lacks a public no-arg constructor, is non-public/abstract, or its constructor throws (NPE, missing dependency, failed config lookup) during bootstrap or runtime listener replacement.","commonSituations":"Listeners designed as Spring beans being registered by class instead of by instance, so dependency injection never happens and constructors fail; constructors performing environment lookups (JNDI, config files) that fail outside the dev environment; refactoring that added constructor parameters without updating a class-based registration path.","solutions":["Register listener instances instead of classes — construct them yourself (or via DI) and call registry.setListeners(eventType, instance), keeping dependencies injected","If class-based registration is required, provide a public no-arg constructor and defer all dependency resolution to first callback","Read the cause chain: NoSuchMethodException -> missing no-arg ctor; InvocationTargetException -> constructor logic failed — fix that code"],"exampleFix":"// before\nregistry.setListenerClasses(EventType.POST_INSERT, MyAuditingListener.class);\n// MyAuditingListener(DataSource ds) only -> Unable to instantiate specified event listener class\n\n// after\nMyAuditingListener listener = new MyAuditingListener(dataSource);\nregistry.setListeners(EventType.POST_INSERT, listener);","handlingStrategy":"validation","validationCode":"try {\n    listenerClass.getConstructor(); // public no-arg constructor present?\n} catch (NoSuchMethodException e) {\n    // register the instance instead of the class:\n    registry.setListeners(eventType, new MyListener(deps));\n    return;\n}\nregistry.setListenerClasses(eventType, listenerClass);","typeGuard":"boolean hasPublicNoArgCtor(Class<?> c) {\n    try { return Modifier.isPublic(c.getConstructor().getModifiers()); }\n    catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":"try {\n    registry.setListenerClasses(EventType.POST_INSERT, MyListener.class);\n} catch (EventListenerRegistrationException e) {\n    Throwable cause = e.getCause();\n    if (cause instanceof NoSuchMethodException) {\n        registry.setListeners(EventType.POST_INSERT, new MyListener()); // instance-based fallback\n    } else { throw e; }\n}","preventionTips":["Prefer instance-based setListeners over class-based registration when listeners need dependencies","Keep listener constructors public, no-arg, and side-effect free","Smoke-test listener construction during application startup, not lazily at first event"],"tags":["hibernate","event-listener","reflection","instantiation","configuration","bootstrap"],"backgroundTag":"config-class-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}