Skip to content

Spring-Shell 4.0.2 (regression from 4.0.1) - No Scope registered for scope name 'request' - Candidate search scope includes wrong beans #1351

@tkrah

Description

@tkrah

Switching to 4.0.2 I am facing this exception:

Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'request'
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:366)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:791)
	at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1471)
	at org.springframework.shell.core.autoconfigure.CommandRegistryAutoConfiguration.registerAnnotatedCommands(CommandRegistryAutoConfiguration.java:61)
	at org.springframework.shell.core.autoconfigure.CommandRegistryAutoConfiguration.commandRegistry(CommandRegistryAutoConfiguration.java:50)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)

Which is correct, my Spring-Shell context is build with:

new SpringApplicationBuilder(ShellApplication.class)
			.web(WebApplicationType.NONE)
			.run(args);

But it seems the 4.0.2 path does trigger the usage of Beans in the context, which depends on the request scope, which I don't use at all in my commands and or the context itself, which worked in 4.0.1.

Edit:

https://github.com/tkrah/spring-sec-14911/tree/ISSUE-1351

With 4.0.1 (and 3.x) you will get a working shell:

 :: Spring Boot ::                (v4.0.6)

2026-05-11 10:34:32,425 demo [main]      INFO  com.example.demo.ShellApplication [StartupInfoLogger.java:54] - Starting ShellApplication using Java 25.0.2 with PID 70397 ( .. )
2026-05-11 10:34:32,429 demo [main]      INFO  com.example.demo.ShellApplication [SpringApplication.java:658] - The following 3 profiles are active: "g_dev", "default", "solr"
2026-05-11 10:34:33,125 demo [main]      WARN  o.springframework.security.core.userdetails.User [User.java:283] - User.withDefaultPasswordEncoder() is considered unsafe for production and is only intended for sample applications.
2026-05-11 10:34:33,429 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.ShellApplication
2026-05-11 10:34:33,431 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.commands.IndexCommand
2026-05-11 10:34:33,433 demo [main]      DEBUG o.s.s.c.c.annotation.support.CommandFactoryBean [CommandFactoryBean.java:101] - Creating command bean for method 'doNothing' defined in class 'com.example.demo.commands.IndexCommand' with name 'hello'
2026-05-11 10:34:33,434 demo [main]      DEBUG o.s.s.c.c.annotation.support.CommandFactoryBean [CommandFactoryBean.java:204] - No ConfigurableConversionService bean found, using a default conversion service.
2026-05-11 10:34:33,435 demo [main]      DEBUG o.s.s.c.c.annotation.support.CommandFactoryBean [CommandFactoryBean.java:193] - No AvailabilityProvider bean found with name '', using always available provider.
2026-05-11 10:34:33,436 demo [main]      DEBUG o.s.s.c.c.annotation.support.CommandFactoryBean [CommandFactoryBean.java:182] - No ExitStatusExceptionMapper bean found with name '', using default exception mapping strategy.
2026-05-11 10:34:33,437 demo [main]      DEBUG o.s.s.c.c.annotation.support.CommandFactoryBean [CommandFactoryBean.java:172] - No Validator bean found, using default validator.
2026-05-11 10:34:33,464 demo [main]      INFO  org.hibernate.validator.internal.util.Version [Version.java:18] - HV000001: Hibernate Validator 9.0.1.Final
2026-05-11 10:34:33,590 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.DemoApplication
2026-05-11 10:34:33,590 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.config.AsyncConfig
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.config.AsyncProvider
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.config.ExecutorConfig
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.config.UserConfig
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.scope.ScopeConfig
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.solr.SolrService
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.util.DemoServiceSessionData
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.util.DemoServiceSessionHelper
2026-05-11 10:34:33,591 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.web.User
2026-05-11 10:34:33,592 demo [main]      DEBUG o.s.s.c.a.CommandRegistryAutoConfiguration [CommandRegistryAutoConfiguration.java:76] - Registering commands from component: com.example.demo.web.UserController
2026-05-11 10:34:33,868 demo [main]      INFO  com.example.demo.ShellApplication [StartupInfoLogger.java:60] - Started ShellApplication in 1.995 seconds (process running for 2.439)
shell:>

With 4.0.2:

....
2026-05-11 10:37:08,612 demo [main]      WARN  o.s.c.a.AnnotationConfigApplicationContext [AbstractApplicationContext.java:629] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commandRegistry' defined in class path resource [org/springframework/shell/core/autoconfigure/CommandRegistryAutoConfiguration.class]: Failed to instantiate [org.springframework.shell.core.command.CommandRegistry]: Factory method 'commandRegistry' threw exception with message: No Scope registered for scope name 'request'
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scriptCommand' defined in class path resource [org/springframework/shell/core/autoconfigure/StandardCommandsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'scriptCommand' parameter 0: Error creating bean with name 'commandRegistry': Requested bean is currently in creation: Is there an unresolvable circular reference or an asynchronous initialization dependency?
2026-05-11 10:37:08,619 demo [main]      INFO  o.s.b.a.logging.ConditionEvaluationReportLogger [ConditionEvaluationReportLogger.java:82] - 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2026-05-11 10:37:08,634 demo [main]      ERROR org.springframework.boot.SpringApplication [SpringApplication.java:863] - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commandRegistry' defined in class path resource [org/springframework/shell/core/autoconfigure/CommandRegistryAutoConfiguration.class]: Failed to instantiate [org.springframework.shell.core.command.CommandRegistry]: Factory method 'commandRegistry' threw exception with message: No Scope registered for scope name 'request'
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scriptCommand' defined in class path resource [org/springframework/shell/core/autoconfigure/StandardCommandsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'scriptCommand' parameter 0: Error creating bean with name 'commandRegistry': Requested bean is currently in creation: Is there an unresolvable circular reference or an asynchronous initialization dependency?
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1362)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1194)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:565)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:525)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:371)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:331)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.instantiateSingleton(DefaultListableBeanFactory.java:1218)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingleton(DefaultListableBeanFactory.java:1184)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:1121)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:994)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:621)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:756)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:445)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:154)
	at com.example.demo.ShellApplication.main(ShellApplication.java:12)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:106)
	at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:64)
	at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:40)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.shell.core.command.CommandRegistry]: Factory method 'commandRegistry' threw exception with message: No Scope registered for scope name 'request'
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:183)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:72)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:152)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
	... 24 common frames omitted
Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'request'
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:366)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:791)
	at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1471)
	at org.springframework.shell.core.autoconfigure.CommandRegistryAutoConfiguration.registerAnnotatedCommands(CommandRegistryAutoConfiguration.java:61)
	at org.springframework.shell.core.autoconfigure.CommandRegistryAutoConfiguration.commandRegistry(CommandRegistryAutoConfiguration.java:50)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:155)

Suspected Reason:

4.0.2 does not scan for candidates anymore and just uses all components found which for sure will fail.

If you want to keep the current bean finding code imho it would be better to bring back the top level annotation with @ShellComponent - that way you can be sure to only get Beans you want to look for @Command methods and the user itself made sure that it does not depend on things which won't be there (e.g. request or session scope).

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/need-triageTeam needs to triage and take a first look

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions