spring-projects/spring-framework · error · IllegalStateException

Lifecycle annotation requires a no-arg method:

Error message

Lifecycle annotation requires a no-arg method: 

What it means

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.

Solutions

  1. Remove all parameters from the annotated lifecycle method — it must be a no-arg method.
  2. If parameters are needed, inject dependencies via @Autowired fields/constructor instead and read them in the no-arg init method.

Example fix

// before
@PostConstruct
public void init(Config config) {
    this.config = config;
}

// after
@Autowired
private Config config;

@PostConstruct
public void init() {
    // use this.config
}
Defensive patterns

Strategy: validation

Validate before calling

// Verify lifecycle-annotated methods have zero parameters at startup
for (Method m : beanClass.getDeclaredMethods()) {
    if (m.isAnnotationPresent(PostConstruct.class)
            || m.isAnnotationPresent(PreDestroy.class)) {
        if (m.getParameterCount() != 0) {
            throw new IllegalStateException(
                "Lifecycle method " + m + " must take no arguments");
        }
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/c43540c410c7938c. Report an issue: GitHub.

Appendix: source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java:436

			Collection<LifecycleMethod> destroyMethodsToUse =
					(checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods);
			return !destroyMethodsToUse.isEmpty();
		}
	}


	/**
	 * Class representing an annotated init or destroy method.
	 */
	private static class LifecycleMethod {

		private final Method method;

		private final String identifier;

		public LifecycleMethod(Method method, Class<?> beanClass) {
			if (method.getParameterCount() != 0) {
				throw new IllegalStateException("Lifecycle annotation requires a no-arg method: " + method);
			}
			this.method = method;
			this.identifier = (isPrivateOrNotVisible(method, beanClass) ?
					ClassUtils.getQualifiedMethodName(method) : method.getName());
		}

		public Method getMethod() {
			return this.method;
		}

		public String getIdentifier() {
			return this.identifier;
		}

		public void invoke(Object target) throws Throwable {
			ReflectionUtils.makeAccessible(this.method);
			this.method.invoke(target);
		}

View on GitHub (pinned to 69bf83ad71)