Spring SimpleCommandLinePropertySource

  • 全路径: org.springframework.core.env.SimpleCommandLinePropertySource
  1. public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {}
  • SimpleCommandLinePropertySource 的 source 类型是 CommandLineArgs 具体解释请看下面分析

CommandLineArgs

两个内部属性

  1. class CommandLineArgs {
  2. /**
  3. * 选项参数列表
  4. */
  5. private final Map<String, List<String>> optionArgs = new HashMap<>();
  6. /**
  7. * 非选项参数列表
  8. */
  9. private final List<String> nonOptionArgs = new ArrayList<>();
  10. }

addOptionArg

添加 选项参数

  1. public void addOptionArg(String optionName, @Nullable String optionValue) {
  2. if (!this.optionArgs.containsKey(optionName)) {
  3. this.optionArgs.put(optionName, new ArrayList<>());
  4. }
  5. if (optionValue != null) {
  6. this.optionArgs.get(optionName).add(optionValue);
  7. }
  8. }

getOptionNames

  • 获取选项参数列表
  1. public Set<String> getOptionNames() {
  2. return Collections.unmodifiableSet(this.optionArgs.keySet());
  3. }
  • 其他方法不具体描述了,各位可以查看下面的代码
  1. class CommandLineArgs {
  2. /**
  3. * 选项参数列表
  4. */
  5. private final Map<String, List<String>> optionArgs = new HashMap<>();
  6. /**
  7. * 非选项参数列表
  8. */
  9. private final List<String> nonOptionArgs = new ArrayList<>();
  10. /**
  11. * Add an option argument for the given option name and add the given value to the
  12. * list of values associated with this option (of which there may be zero or more).
  13. * The given value may be {@code null}, indicating that the option was specified
  14. * without an associated value (e.g. "--foo" vs. "--foo=bar").
  15. *
  16. * 添加 选项参数
  17. */
  18. public void addOptionArg(String optionName, @Nullable String optionValue) {
  19. if (!this.optionArgs.containsKey(optionName)) {
  20. this.optionArgs.put(optionName, new ArrayList<>());
  21. }
  22. if (optionValue != null) {
  23. this.optionArgs.get(optionName).add(optionValue);
  24. }
  25. }
  26. /**
  27. * Return the set of all option arguments present on the command line.
  28. * 获取选项参数列表
  29. */
  30. public Set<String> getOptionNames() {
  31. return Collections.unmodifiableSet(this.optionArgs.keySet());
  32. }
  33. /**
  34. * Return whether the option with the given name was present on the command line.
  35. */
  36. public boolean containsOption(String optionName) {
  37. return this.optionArgs.containsKey(optionName);
  38. }
  39. /**
  40. * Return the list of values associated with the given option. {@code null} signifies
  41. * that the option was not present; empty list signifies that no values were associated
  42. * with this option.
  43. */
  44. @Nullable
  45. public List<String> getOptionValues(String optionName) {
  46. return this.optionArgs.get(optionName);
  47. }
  48. /**
  49. * Add the given value to the list of non-option arguments.
  50. */
  51. public void addNonOptionArg(String value) {
  52. this.nonOptionArgs.add(value);
  53. }
  54. /**
  55. * Return the list of non-option arguments specified on the command line.
  56. */
  57. public List<String> getNonOptionArgs() {
  58. return Collections.unmodifiableList(this.nonOptionArgs);
  59. }
  60. }

在了解 CommandLineArgs 类后再来看 SimpleCommandLinePropertySource 会相对容易. 内部的几个方法就是调用 CommandLineArgs 所提供的方法

  1. @Override
  2. public String[] getPropertyNames() {
  3. return StringUtils.toStringArray(this.source.getOptionNames());
  4. }
  5. @Override
  6. protected boolean containsOption(String name) {
  7. return this.source.containsOption(name);
  8. }
  9. @Override
  10. @Nullable
  11. protected List<String> getOptionValues(String name) {
  12. return this.source.getOptionValues(name);
  13. }
  14. @Override
  15. protected List<String> getNonOptionArgs() {
  16. return this.source.getNonOptionArgs();
  17. }