{"record":{"id":"c43540c410c7938c","repo":"spring-projects/spring-framework","slug":"lifecycle-annotation-requires-a-no-arg-method","errorCode":null,"errorMessage":"Lifecycle annotation requires a no-arg method: ","messagePattern":"Lifecycle annotation requires a no-arg method: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java","lineNumber":436,"sourceCode":"\t\t\tCollection<LifecycleMethod> destroyMethodsToUse =\n\t\t\t\t\t(checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods);\n\t\t\treturn !destroyMethodsToUse.isEmpty();\n\t\t}\n\t}\n\n\n\t/**\n\t * Class representing an annotated init or destroy method.\n\t */\n\tprivate static class LifecycleMethod {\n\n\t\tprivate final Method method;\n\n\t\tprivate final String identifier;\n\n\t\tpublic LifecycleMethod(Method method, Class<?> beanClass) {\n\t\t\tif (method.getParameterCount() != 0) {\n\t\t\t\tthrow new IllegalStateException(\"Lifecycle annotation requires a no-arg method: \" + method);\n\t\t\t}\n\t\t\tthis.method = method;\n\t\t\tthis.identifier = (isPrivateOrNotVisible(method, beanClass) ?\n\t\t\t\t\tClassUtils.getQualifiedMethodName(method) : method.getName());\n\t\t}\n\n\t\tpublic Method getMethod() {\n\t\t\treturn this.method;\n\t\t}\n\n\t\tpublic String getIdentifier() {\n\t\t\treturn this.identifier;\n\t\t}\n\n\t\tpublic void invoke(Object target) throws Throwable {\n\t\t\tReflectionUtils.makeAccessible(this.method);\n\t\t\tthis.method.invoke(target);\n\t\t}","sourceCodeStart":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java#L418-L454","documentation":"Thrown by the LifecycleMethod constructor in InitDestroyAnnotationBeanPostProcessor when an init or destroy annotation is placed on a method that declares parameters. Lifecycle (init/destroy) methods must have zero parameters; the check at line 435 rejects any method with parameterCount != 0.","triggerScenarios":"Annotating a method that accepts arguments with @PostConstruct, @PreDestroy, or a custom lifecycle annotation. For example: @PostConstruct public void init(String config) { ... } — the single parameter triggers the error.","commonSituations":"Adding parameters to an init method during refactoring without realizing the annotation constraint. Using @PostConstruct on a method that previously took config arguments. Custom lifecycle annotations placed on methods that happen to have parameters.","solutions":["Remove all parameters from the annotated lifecycle method — it must be a no-arg method.","If parameters are needed, inject dependencies via @Autowired fields/constructor instead and read them in the no-arg init method."],"exampleFix":"// before\n@PostConstruct\npublic void init(Config config) {\n    this.config = config;\n}\n\n// after\n@Autowired\nprivate Config config;\n\n@PostConstruct\npublic void init() {\n    // use this.config\n}","handlingStrategy":"validation","validationCode":"// Verify lifecycle-annotated methods have zero parameters at startup\nfor (Method m : beanClass.getDeclaredMethods()) {\n    if (m.isAnnotationPresent(PostConstruct.class)\n            || m.isAnnotationPresent(PreDestroy.class)) {\n        if (m.getParameterCount() != 0) {\n            throw new IllegalStateException(\n                \"Lifecycle method \" + m + \" must take no arguments\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Lifecycle methods (@PostConstruct, @PreDestroy, custom init/destroy annotations) must be no-arg.","Inject dependencies via fields or constructor, not via init method parameters.","Add a reflection-based test to verify all lifecycle methods have zero parameters."],"tags":["spring","lifecycle","init-method","annotation"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}