Skip to content

Commit f6394a4

Browse files
committed
Rework bean handling
- Lot of rework to move better model to work around bean cycles - Remove use of @lazy - Move StandardAPIAutoConfiguration to autoconfig package - Remove some of a direct ObjectProvider use in constructors - Adds spring-native support with most of a things working out of a box - Relates spring-projects#324 - Relates spring-projects#329 - Relates spring-projects#323
1 parent 2706da3 commit f6394a4

File tree

33 files changed

+342
-168
lines changed

33 files changed

+342
-168
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ApplicationRunnerAutoConfiguration.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import org.jline.reader.LineReader;
1919
import org.jline.reader.Parser;
2020

21-
import org.springframework.beans.factory.annotation.Autowired;
22-
import org.springframework.boot.ApplicationRunner;
2321
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2422
import org.springframework.context.annotation.Bean;
2523
import org.springframework.context.annotation.Configuration;
@@ -33,27 +31,30 @@
3331
import static org.springframework.shell.jline.InteractiveShellApplicationRunner.SPRING_SHELL_INTERACTIVE;
3432
import static org.springframework.shell.jline.ScriptShellApplicationRunner.SPRING_SHELL_SCRIPT;
3533

36-
@Configuration
34+
@Configuration(proxyBeanMethods = false)
3735
public class ApplicationRunnerAutoConfiguration {
3836

39-
@Autowired
4037
private Shell shell;
4138

42-
@Autowired
4339
private PromptProvider promptProvider;
4440

45-
@Autowired
4641
private LineReader lineReader;
4742

43+
public ApplicationRunnerAutoConfiguration(Shell shell, PromptProvider promptProvider, LineReader lineReader) {
44+
this.shell = shell;
45+
this.promptProvider = promptProvider;
46+
this.lineReader = lineReader;
47+
}
48+
4849
@Bean
4950
@ConditionalOnProperty(prefix = SPRING_SHELL_INTERACTIVE, value = InteractiveShellApplicationRunner.ENABLED, havingValue = "true", matchIfMissing = true)
50-
public ApplicationRunner interactiveApplicationRunner(Environment environment) {
51+
public InteractiveShellApplicationRunner interactiveApplicationRunner(Environment environment) {
5152
return new InteractiveShellApplicationRunner(lineReader, promptProvider, shell, environment);
5253
}
5354

5455
@Bean
5556
@ConditionalOnProperty(prefix = SPRING_SHELL_SCRIPT, value = ScriptShellApplicationRunner.ENABLED, havingValue = "true", matchIfMissing = true)
56-
public ApplicationRunner scriptApplicationRunner(Parser parser, ConfigurableEnvironment environment) {
57+
public ScriptShellApplicationRunner scriptApplicationRunner(Parser parser, ConfigurableEnvironment environment) {
5758
return new ScriptShellApplicationRunner(parser, shell, environment);
5859
}
5960
}

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/CommandRegistryAutoConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class CommandRegistryAutoConfiguration {
2727

2828
@Bean
2929
public CommandRegistry commandRegistry(
30-
ObjectProvider<MethodTargetRegistrar> methodTargerRegistrars) {
30+
ObjectProvider<MethodTargetRegistrar> methodTargetRegistrars) {
3131
ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
32-
methodTargerRegistrars.orderedStream().forEach(resolver -> {
32+
methodTargetRegistrars.orderedStream().forEach(resolver -> {
3333
resolver.register(registry);
3434
});
3535
return registry;

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/CompleterAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.shell.CompletionProposal;
3232
import org.springframework.shell.Shell;
3333

34-
@Configuration
34+
@Configuration(proxyBeanMethods = false)
3535
public class CompleterAutoConfiguration {
3636

3737
@Autowired

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/JCommanderParameterResolverAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @author Eric Bottard
3030
*/
31-
@Configuration
31+
@Configuration(proxyBeanMethods = false)
3232
@ConditionalOnClass({ JCommander.class, JCommanderParameterResolver.class })
3333
public class JCommanderParameterResolverAutoConfiguration {
3434

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/JLineAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
2424

25-
@Configuration
25+
@Configuration(proxyBeanMethods = false)
2626
public class JLineAutoConfiguration {
2727

2828
@Configuration

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/JLineShellAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @author Eric Bottard
3838
* @author Florent Biville
3939
*/
40-
@Configuration
40+
@Configuration(proxyBeanMethods = false)
4141
public class JLineShellAutoConfiguration {
4242

4343
@Bean(destroyMethod = "close")

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/LineReaderAutoConfiguration.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,43 @@
2929
import org.jline.utils.AttributedStringBuilder;
3030
import org.jline.utils.AttributedStyle;
3131

32-
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.beans.factory.annotation.Value;
3433
import org.springframework.context.annotation.Bean;
3534
import org.springframework.context.annotation.Configuration;
3635
import org.springframework.context.event.ContextClosedEvent;
3736
import org.springframework.context.event.EventListener;
3837
import org.springframework.shell.CommandRegistry;
3938

40-
@Configuration
39+
@Configuration(proxyBeanMethods = false)
4140
public class LineReaderAutoConfiguration {
4241

43-
@Autowired
4442
private Terminal terminal;
4543

46-
@Autowired
4744
private Completer completer;
4845

49-
@Autowired
5046
private Parser parser;
5147

52-
@Autowired
5348
private CommandRegistry commandRegistry;
5449

55-
@Autowired
5650
private org.jline.reader.History jLineHistory;
5751

52+
@Value("${spring.application.name:spring-shell}.log")
53+
private String historyPath;
54+
55+
public LineReaderAutoConfiguration(Terminal terminal, Completer completer, Parser parser,
56+
CommandRegistry commandRegistry, org.jline.reader.History jLineHistory) {
57+
this.terminal = terminal;
58+
this.completer = completer;
59+
this.parser = parser;
60+
this.commandRegistry = commandRegistry;
61+
this.jLineHistory = jLineHistory;
62+
}
63+
5864
@EventListener
5965
public void onContextClosedEvent(ContextClosedEvent event) throws IOException {
6066
jLineHistory.save();
6167
}
6268

63-
@Value("${spring.application.name:spring-shell}.log")
64-
private String historyPath;
65-
6669
@Bean
6770
public LineReader lineReader() {
6871
LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.shell.boot;
2+
3+
import java.util.Set;
4+
import java.util.stream.Collectors;
5+
6+
import org.springframework.beans.factory.ObjectProvider;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.core.convert.ConversionService;
11+
import org.springframework.shell.ParameterResolver;
12+
import org.springframework.shell.standard.StandardParameterResolver;
13+
import org.springframework.shell.standard.ValueProvider;
14+
15+
@Configuration(proxyBeanMethods = false)
16+
public class ParameterResolverAutoConfiguration {
17+
18+
@Bean
19+
public ParameterResolver standardParameterResolver(@Qualifier("spring-shell") ConversionService conversionService,
20+
ObjectProvider<ValueProvider> valueProviders) {
21+
Set<ValueProvider> collect = valueProviders.orderedStream().collect(Collectors.toSet());
22+
return new StandardParameterResolver(conversionService, collect);
23+
}
24+
25+
}

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import java.util.Collection;
2020
import java.util.Set;
2121

22-
import javax.validation.Validation;
23-
import javax.validation.Validator;
24-
2522
import org.springframework.beans.factory.annotation.Qualifier;
26-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2723
import org.springframework.context.ApplicationContext;
2824
import org.springframework.context.annotation.Bean;
2925
import org.springframework.context.annotation.Configuration;
@@ -33,6 +29,7 @@
3329
import org.springframework.core.convert.converter.ConverterFactory;
3430
import org.springframework.core.convert.converter.GenericConverter;
3531
import org.springframework.core.convert.support.DefaultConversionService;
32+
import org.springframework.shell.CommandRegistry;
3633
import org.springframework.shell.ResultHandler;
3734
import org.springframework.shell.ResultHandlerService;
3835
import org.springframework.shell.Shell;
@@ -42,7 +39,7 @@
4239
/**
4340
* Creates supporting beans for running the Shell
4441
*/
45-
@Configuration
42+
@Configuration(proxyBeanMethods = false)
4643
@Import(ResultHandlerConfig.class)
4744
public class SpringShellAutoConfiguration {
4845

@@ -66,12 +63,6 @@ public ConversionService shellConversionService(ApplicationContext applicationCo
6663
return defaultConversionService;
6764
}
6865

69-
@Bean
70-
@ConditionalOnMissingBean(Validator.class)
71-
public Validator validator() {
72-
return Validation.buildDefaultValidatorFactory().getValidator();
73-
}
74-
7566
@Bean
7667
public ResultHandlerService resultHandlerService(Set<ResultHandler<?>> resultHandlers) {
7768
GenericResultHandlerService service = new GenericResultHandlerService();
@@ -82,7 +73,7 @@ public ResultHandlerService resultHandlerService(Set<ResultHandler<?>> resultHan
8273
}
8374

8475
@Bean
85-
public Shell shell(ResultHandlerService resultHandlerService) {
86-
return new Shell(resultHandlerService);
76+
public Shell shell(ResultHandlerService resultHandlerService, CommandRegistry commandRegistry) {
77+
return new Shell(resultHandlerService, commandRegistry);
8778
}
8879
}

spring-shell-standard/src/main/java/org/springframework/shell/standard/StandardAPIAutoConfiguration.java spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/StandardAPIAutoConfiguration.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.shell.standard;
17+
package org.springframework.shell.boot;
1818

19-
import org.springframework.beans.factory.annotation.Qualifier;
2019
import org.springframework.context.annotation.Bean;
2120
import org.springframework.context.annotation.Configuration;
22-
import org.springframework.context.annotation.Lazy;
23-
import org.springframework.core.convert.ConversionService;
2421
import org.springframework.shell.CommandRegistry;
2522
import org.springframework.shell.MethodTargetRegistrar;
26-
import org.springframework.shell.ParameterResolver;
23+
import org.springframework.shell.standard.CommandValueProvider;
24+
import org.springframework.shell.standard.EnumValueProvider;
25+
import org.springframework.shell.standard.FileValueProvider;
26+
import org.springframework.shell.standard.StandardMethodTargetRegistrar;
27+
import org.springframework.shell.standard.ValueProvider;
2728

2829
/**
2930
* Sets up all required beans for supporting the standard Shell API.
3031
*
3132
* @author Eric Bottard
3233
*/
33-
@Configuration
34+
@Configuration(proxyBeanMethods = false)
3435
public class StandardAPIAutoConfiguration {
3536

3637
@Bean
37-
public ValueProvider commandValueProvider(@Lazy CommandRegistry commandRegistry) {
38+
public ValueProvider commandValueProvider(CommandRegistry commandRegistry) {
3839
return new CommandValueProvider(commandRegistry);
3940
}
4041

@@ -52,9 +53,4 @@ public ValueProvider fileValueProvider() {
5253
public MethodTargetRegistrar standardMethodTargetResolver() {
5354
return new StandardMethodTargetRegistrar();
5455
}
55-
56-
@Bean
57-
public ParameterResolver standardParameterResolver(@Qualifier("spring-shell") ConversionService conversionService) {
58-
return new StandardParameterResolver(conversionService);
59-
}
6056
}

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/StandardCommandsAutoConfiguration.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.shell.boot;
1818

19-
import java.util.List;
20-
2119
import org.jline.reader.Parser;
2220

2321
import org.springframework.beans.factory.ObjectProvider;
@@ -26,8 +24,7 @@
2624
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2725
import org.springframework.context.annotation.Bean;
2826
import org.springframework.context.annotation.Configuration;
29-
import org.springframework.shell.ParameterResolver;
30-
import org.springframework.shell.Shell;
27+
import org.springframework.shell.result.ThrowableResultHandler;
3128
import org.springframework.shell.standard.commands.Clear;
3229
import org.springframework.shell.standard.commands.Help;
3330
import org.springframework.shell.standard.commands.History;
@@ -40,15 +37,15 @@
4037
*
4138
* @author Eric Bottard
4239
*/
43-
@Configuration
40+
@Configuration(proxyBeanMethods = false)
4441
@ConditionalOnClass({ Help.Command.class })
4542
public class StandardCommandsAutoConfiguration {
4643

4744
@Bean
4845
@ConditionalOnMissingBean(Help.Command.class)
4946
@ConditionalOnProperty(prefix = "spring.shell.command.help", value = "enabled", havingValue = "true", matchIfMissing = true)
50-
public Help help(List<ParameterResolver> parameterResolvers) {
51-
return new Help(parameterResolvers);
47+
public Help help() {
48+
return new Help();
5249
}
5350

5451
@Bean
@@ -68,15 +65,15 @@ public Quit quit() {
6865
@Bean
6966
@ConditionalOnMissingBean(Stacktrace.Command.class)
7067
@ConditionalOnProperty(prefix = "spring.shell.command.stacktrace", value = "enabled", havingValue = "true", matchIfMissing = true)
71-
public Stacktrace stacktrace() {
72-
return new Stacktrace();
68+
public Stacktrace stacktrace(ObjectProvider<ThrowableResultHandler> throwableResultHandler) {
69+
return new Stacktrace(throwableResultHandler);
7370
}
7471

7572
@Bean
7673
@ConditionalOnMissingBean(Script.Command.class)
7774
@ConditionalOnProperty(prefix = "spring.shell.command.script", value = "enabled", havingValue = "true", matchIfMissing = true)
78-
public Script script(ObjectProvider<Shell> shell, Parser parser) {
79-
return new Script(shell, parser);
75+
public Script script(Parser parser) {
76+
return new Script(parser);
8077
}
8178

8279
@Bean

spring-shell-autoconfigure/src/main/resources/META-INF/spring.factories

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ org.springframework.shell.boot.CompleterAutoConfiguration,\
77
org.springframework.shell.boot.JLineAutoConfiguration,\
88
org.springframework.shell.boot.JLineShellAutoConfiguration,\
99
org.springframework.shell.boot.JCommanderParameterResolverAutoConfiguration,\
10+
org.springframework.shell.boot.ParameterResolverAutoConfiguration,\
11+
org.springframework.shell.boot.StandardAPIAutoConfiguration,\
1012
org.springframework.shell.boot.StandardCommandsAutoConfiguration

spring-shell-core/src/main/java/org/springframework/shell/Shell.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import javax.annotation.PostConstruct;
3232
import javax.validation.ConstraintViolation;
33-
import javax.validation.Validation;
3433
import javax.validation.Validator;
3534
import javax.validation.ValidatorFactory;
3635

@@ -70,10 +69,9 @@ public class Shell {
7069
@Autowired
7170
protected ApplicationContext applicationContext;
7271

73-
@Autowired
7472
private CommandRegistry commandRegistry;
7573

76-
private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
74+
private Validator validator = Utils.defaultValidator();
7775

7876
protected Map<String, MethodTarget> methodTargets = new HashMap<>();
7977

@@ -85,8 +83,9 @@ public class Shell {
8583
*/
8684
protected static final Object UNRESOLVED = new Object();
8785

88-
public Shell(ResultHandlerService resultHandlerService) {
86+
public Shell(ResultHandlerService resultHandlerService, CommandRegistry commandRegistry) {
8987
this.resultHandlerService = resultHandlerService;
88+
this.commandRegistry = commandRegistry;
9089
}
9190

9291
@Autowired(required = false)

0 commit comments

Comments
 (0)