Spring CommandLinePropertySource

  • Author: HuiFer
  • 源码阅读仓库: SourceHot-spring

  • 类全路径: org.springframework.core.env.CommandLinePropertySource

  • 作用: 用来存储命令行参数
  1. public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T> {
  2. public static final String COMMAND_LINE_PROPERTY_SOURCE_NAME = "commandLineArgs";
  3. public static final String DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME = "nonOptionArgs";
  4. private String nonOptionArgsPropertyName = DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;
  5. public CommandLinePropertySource(T source) {
  6. // 命令行参数, 属性值
  7. super(COMMAND_LINE_PROPERTY_SOURCE_NAME, source);
  8. }
  9. public CommandLinePropertySource(String name, T source) {
  10. // 参数名称, 参数值
  11. super(name, source);
  12. }
  13. public void setNonOptionArgsPropertyName(String nonOptionArgsPropertyName) {
  14. this.nonOptionArgsPropertyName = nonOptionArgsPropertyName;
  15. }
  16. @Override
  17. public final boolean containsProperty(String name) {
  18. // 输入值是否等于nonOptionArgs
  19. if (this.nonOptionArgsPropertyName.equals(name)) {
  20. // 等于后判断参数列表是否为空
  21. return !this.getNonOptionArgs().isEmpty();
  22. }
  23. // 是否存在 name 属性
  24. return this.containsOption(name);
  25. }
  26. @Override
  27. @Nullable
  28. public final String getProperty(String name) {
  29. if (this.nonOptionArgsPropertyName.equals(name)) {
  30. // 获取 非可选项参数列表
  31. Collection<String> nonOptionArguments = this.getNonOptionArgs();
  32. if (nonOptionArguments.isEmpty()) {
  33. return null;
  34. }
  35. else {
  36. // 可选参数命令行参数
  37. return StringUtils.collectionToCommaDelimitedString(nonOptionArguments);
  38. }
  39. }
  40. Collection<String> optionValues = this.getOptionValues(name);
  41. if (optionValues == null) {
  42. return null;
  43. }
  44. else {
  45. // 命令行参数
  46. return StringUtils.collectionToCommaDelimitedString(optionValues);
  47. }
  48. }
  49. /**
  50. * 是否存在 name 的命令行参数
  51. */
  52. protected abstract boolean containsOption(String name);
  53. /**
  54. * 获取参数列表集合
  55. */
  56. @Nullable
  57. protected abstract List<String> getOptionValues(String name);
  58. /**
  59. * 获取 non-option 参数列表
  60. */
  61. protected abstract List<String> getNonOptionArgs();
  62. }

getOptionValues

  1. /**
  2. * Return the collection of values associated with the command line option having the
  3. * given name.
  4. * <ul>
  5. * <li>if the option is present and has no argument (e.g.: "--foo"), return an empty
  6. * collection ({@code []})</li>
  7. * <li>if the option is present and has a single value (e.g. "--foo=bar"), return a
  8. * collection having one element ({@code ["bar"]})</li>
  9. * <li>if the option is present and the underlying command line parsing library
  10. * supports multiple arguments (e.g. "--foo=bar --foo=baz"), return a collection
  11. * having elements for each value ({@code ["bar", "baz"]})</li>
  12. * <li>if the option is not present, return {@code null}</li>
  13. * </ul>
  14. *
  15. * 获取参数列表集合
  16. */
  17. @Nullable
  18. protected abstract List<String> getOptionValues(String name);

阅读注释可以知道该方法可以获取命令行参数的列表.

  • --foo作为开头当输入命令行为 --foo=bar --foo=baz 在输入参数名称 foo 会得到数据bar,baz