Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.springframework.shell.core.command.CommandRegistry;
import org.springframework.shell.core.command.annotation.support.CommandFactoryBean;
import org.springframework.shell.core.utils.Utils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import static org.springframework.shell.core.utils.Utils.isProfileActive;
Expand All @@ -58,17 +57,20 @@ private void registerProgrammaticCommands(ApplicationContext applicationContext,
}

private void registerAnnotatedCommands(ApplicationContext applicationContext, CommandRegistry commandRegistry) {
Map<String, Object> components = applicationContext.getBeansWithAnnotation(Component.class);
for (Object candidateComponent : components.values()) {
Class<?> type = candidateComponent.getClass();
ReflectionUtils.MethodFilter filter = method -> AnnotatedElementUtils.hasAnnotation(method,
org.springframework.shell.core.command.annotation.Command.class)
&& isProfileActive(method, applicationContext.getEnvironment());

for (String beanName : applicationContext.getBeanDefinitionNames()) {
Class<?> type = applicationContext.getType(beanName, false);
if (type == null) {
continue;
}
String className = type.getName();
if (className.startsWith("org.springframework.boot")) {
continue;
}
log.debug("Registering commands from component: " + className);
ReflectionUtils.MethodFilter filter = method -> AnnotatedElementUtils.hasAnnotation(method,
org.springframework.shell.core.command.annotation.Command.class)
&& isProfileActive(method, applicationContext.getEnvironment());
Set<Method> methods = MethodIntrospector.selectMethods(type, filter);
for (Method method : methods) {
CommandFactoryBean factoryBean = new CommandFactoryBean(method);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2026-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.core.autoconfigure;

import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.shell.core.command.CommandRegistry;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.stereotype.Component;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for {@link CommandRegistryAutoConfiguration} command discovery.
*
* @author David Pilar
*/
class CommandRegistryAutoConfigurationTests {

/**
* Regression test for
* <a href= "https://github.com/spring-projects/spring-shell/issues/1351">gh-1351</a>:
* registering annotated commands must not eagerly instantiate beans whose scope (e.g.
* {@code request}) is not available in a non-web shell application.
*/
@Test
void requestScopedBeanDoesNotPreventCommandRegistration() {
new ApplicationContextRunner().withUserConfiguration(TestConfiguration.class, RequestScopedBean.class)
.withConfiguration(AutoConfigurations.of(SpringShellAutoConfiguration.class))
.run(context -> {
assertNull(context.getStartupFailure(), "Context should start without errors");
CommandRegistry registry = context.getBean(CommandRegistry.class);
assertTrue(registry.getCommands().stream().anyMatch(c -> c.getName().equals("greet")),
"Annotated command should be registered");
});
}

@Configuration
static class TestConfiguration {

@Command(name = "greet", description = "Say hello")
public void sayHello() {
}

}

@Component
@Scope("request")
static class RequestScopedBean {

}

}
Loading