T
- ソース型 public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T>
PropertySource
実装の抽象基本クラス。パラメーター化された型 T
は、コマンドラインオプションの基になるソースを表します。これは、SimpleCommandLinePropertySource
の場合は String 配列のように単純な場合もあれば、JOptCommandLinePropertySource
の場合は JOpt の OptionSet
などの特定の API に固有の場合もあります。String[]
を受け入れる従来の main
メソッドを介してブートストラップされるアプリケーションで使用するため。多くの場合、main
メソッド内で直接コマンドライン引数を処理することで十分ですが、他の場合では、引数を値として Spring Bean に挿入することが望ましい場合があります。CommandLinePropertySource
が役立つのは、後者の場合です。CommandLinePropertySource
は通常、Spring ApplicationContext
の Environment
に追加されます。この時点で、すべてのコマンドライン引数が PropertyResolver.getProperty(String)
ファミリのメソッドを通じて使用可能になります。例:public static void main(String[] args) { CommandLinePropertySource clps = ...; AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().getPropertySources().addFirst(clps); ctx.register(AppConfig.class); ctx.refresh(); }上記のブートストラップロジックを使用すると、
AppConfig
クラスは Spring Environment
を @Inject
し、プロパティを直接照会できます。@Configuration public class AppConfig { @Inject Environment env; @Bean public void DataSource dataSource() { MyVendorDataSource dataSource = new MyVendorDataSource(); dataSource.setHostname(env.getProperty("db.hostname", "localhost")); dataSource.setUsername(env.getRequiredProperty("db.username")); dataSource.setPassword(env.getRequiredProperty("db.password")); // ... return dataSource; } }
CommandLinePropertySource
は #addFirst
メソッドを使用して Environment
の MutablePropertySources
のセットに追加されたため、検索の優先順位が最も高くなります。つまり、"db.hostname" およびその他のプロパティは、システム環境変数などの他のプロパティソースに存在する可能性がありますが、最初にコマンドラインプロパティソース。コマンドラインで指定された引数が環境変数として指定された引数よりも当然より具体的であることを考えると、これは合理的なアプローチです。Environment
を注入する代わりに、Spring の @Value
アノテーションを使用して、直接または <context:property-placeholder>
要素を使用して PropertySourcesPropertyResolver
Bean が登録されている場合に、これらのプロパティを注入できます。例:
@Component public class MyComponent { @Value("my.property:defaultVal") private String myProperty; public void getMyProperty() { return this.myProperty; } // ... }
個々のコマンドライン引数は、通常の PropertySource.getProperty(String)
および PropertySource.containsProperty(String)
メソッドを通じてプロパティとして表されます。例: 次のコマンドラインを指定します。
--o1=v1 --o2"o1" と "o2" は「オプション引数」として扱われ、次のアサーションは true と評価されます。
CommandLinePropertySource<?> ps = ... assert ps.containsProperty("o1") == true; assert ps.containsProperty("o2") == true; assert ps.containsProperty("o3") == false; assert ps.getProperty("o1").equals("v1"); assert ps.getProperty("o2").equals(""); assert ps.getProperty("o3") == null;'o2' オプションには引数はありませんが、
getProperty("o2")
は null
ではなく空の文字列(""
)に解決されますが、getProperty("o3")
は指定されなかったため null
に解決されます。この動作は、すべての PropertySource
実装が従う一般規約と一致しています。上記の例ではオプション引数を示すために "-" が使用されていましたが、この構文は個々のコマンドライン引数ライブラリによって異なる場合があることにも注意してください。例: JOpt または Commons の CLI ベースの実装では、単一のダッシュ( "-" )、「短い」オプションの引数などが許可される場合があります。
この抽象化により、オプション以外の引数もサポートされます。"-" や "-" などのオプション形式の接頭辞なしで指定された引数は「非オプション引数」と見なされ、特別な "nonOptionArgs" プロパティを通じて使用できます。オプション以外の引数が複数指定されている場合、このプロパティの値は、すべての引数を含むコンマ区切りの文字列になります。このアプローチは、CommandLinePropertySource
からのすべてのプロパティの単純で一貫した戻り値の型(文字列)を保証し、同時に Spring Environment
およびその組み込み ConversionService
と組み合わせて使用すると変換に役立ちます。次の例について考えてみます。
--o1=v1 --o2=v2 /path/to/file1 /path/to/file2この例では、"o1" と "o2" は「オプション引数」と見なされますが、2 つのファイルシステムパスは「非オプション引数」と見なされます。そのため、次のアサーションは true と評価されます。
CommandLinePropertySource<?> ps = ... assert ps.containsProperty("o1") == true; assert ps.containsProperty("o2") == true; assert ps.containsProperty("nonOptionArgs") == true; assert ps.getProperty("o1").equals("v1"); assert ps.getProperty("o2").equals("v2"); assert ps.getProperty("nonOptionArgs").equals("/path/to/file1,/path/to/file2");
上記のように、Spring Environment
抽象化と組み合わせて使用すると、このカンマ区切りの文字列は、簡単に文字列配列またはリストに変換できます。
Environment env = applicationContext.getEnvironment(); String[] nonOptionArgs = env.getProperty("nonOptionArgs", String[].class); assert nonOptionArgs[0].equals("/path/to/file1"); assert nonOptionArgs[1].equals("/path/to/file2");
特別な「オプション以外の引数」プロパティの名前は、setNonOptionArgsPropertyName(String)
メソッドを使用してカスタマイズできます。オプション以外の引数に適切なセマンティック値を与えるため、そうすることをお勧めします。例: ファイルシステムパスがオプション以外の引数として指定されている場合、これらをデフォルトの "nonOptionArgs" よりも "file.locations" のようなものとして参照することをお勧めします。
public static void main(String[] args) { CommandLinePropertySource clps = ...; clps.setNonOptionArgsPropertyName("file.locations"); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().getPropertySources().addFirst(clps); ctx.register(AppConfig.class); ctx.refresh(); }
String[]
を解析し、解析結果を CommandLinePropertySource
の実装に提供するだけです。その時点で、すべての引数は「オプション」または「非オプション」引数のいずれかと見なすことができ、上記のように、通常の PropertySource
および Environment
API を介してアクセスできます。PropertySource
, SimpleCommandLinePropertySource
, JOptCommandLinePropertySource
PropertySource.StubPropertySource
修飾子と型 | フィールドと説明 |
---|---|
static StringSE | COMMAND_LINE_PROPERTY_SOURCE_NAME CommandLinePropertySource インスタンスに付けられたデフォルト名: "commandLineArgs"。 |
static StringSE | DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME 非オプション引数を表すプロパティのデフォルト名: "nonOptionArgs"。 |
logger, name, source
コンストラクターと説明 |
---|
CommandLinePropertySource(StringSE name, T source) 指定された名前を持ち、指定されたソースオブジェクトに基づく新しい CommandLinePropertySource を作成します。 |
CommandLinePropertySource(T source) デフォルトの名前 "commandLineArgs" を持ち、指定されたソースオブジェクトに基づく新しい CommandLinePropertySource を作成します。 |
修飾子と型 | メソッドと説明 |
---|---|
protected abstract boolean | containsOption(StringSE name) コマンドラインから解析されたオプション引数のセットに、指定された名前のオプションが含まれているかどうかを返します。 |
boolean | containsProperty(StringSE name) この実装は最初に、指定された名前が特別な「非オプション引数」プロパティであるかどうかを確認し、そうである場合は、抽象 getNonOptionArgs() メソッドチェックに委譲して、空のコレクションを返すかどうかを確認します。 |
protected abstract ListSE<StringSE> | getNonOptionArgs() コマンドラインから解析された非オプション引数のコレクションを返します。 |
protected abstract ListSE<StringSE> | getOptionValues(StringSE name) 指定された名前を持つコマンドラインオプションに関連付けられた値のコレクションを返します。 |
StringSE | getProperty(StringSE name) この実装は最初に、指定された名前が特別な「非オプション引数」プロパティであるかどうかを確認し、そうである場合は抽象 getNonOptionArgs() メソッドに委譲します。 |
void | setNonOptionArgsPropertyName(StringSE nonOptionArgsPropertyName) 特別な「非オプション引数」プロパティの名前を指定します。 |
getPropertyNames
equals, getName, getSource, hashCode, named, toString
cloneSE, finalizeSE, getClassSE, notifySE, notifyAllSE, waitSE, waitSE, waitSE
public static final StringSE COMMAND_LINE_PROPERTY_SOURCE_NAME
CommandLinePropertySource
インスタンスに付けられたデフォルト名: "commandLineArgs"。public CommandLinePropertySource(T source)
CommandLinePropertySource
を作成します。public CommandLinePropertySource(StringSE name, T source)
CommandLinePropertySource
を作成します。public void setNonOptionArgsPropertyName(StringSE nonOptionArgsPropertyName)
public final boolean containsProperty(StringSE name)
getNonOptionArgs()
メソッドチェックに委譲して、空のコレクションを返すかどうかを確認します。それ以外の場合は、抽象 containsOption(String)
メソッドに委譲してその値を返します。EnumerablePropertySource<T>
の containsProperty
name
- 検索するプロパティの名前 @Nullable public final StringSE getProperty(StringSE name)
getNonOptionArgs()
メソッドに委譲します。そうであり、非オプション引数のコレクションが空の場合、このメソッドは null
を返します。空でない場合、すべての非オプション引数のコンマ区切りの文字列を返します。それ以外の場合は、抽象 getOptionValues(String)
メソッドに委譲してその結果を返します。PropertySource<T>
の getProperty
name
- 検索するプロパティ PropertyResolver.getRequiredProperty(String)
protected abstract boolean containsOption(StringSE name)
@Nullable protected abstract ListSE<StringSE> getOptionValues(StringSE name)
[]
)["bar"]
)["bar", "baz"]
)null
を返します