2017-02-04 6 views
3

SpringフレームワークメソッドPropertyResolver.getProperty(String key, Class targetType)targetTypeパラメータでサポートされている値は何ですか?私はデフォルトでサポートされているタイプのリストを探しています。Springにはどのようなプロパティタイプコンバータが組み込まれていますか?

次の種類が明らかに動作します:

  • Locale.class
  • File.class
  • int.classを
  • URI.class

例:

// works 
Locale myLocale = propertyResolver.getProperty("my.locale", Locale.class); 

このリストが見つかりました: http://www.logicbig.com/how-to/spring-framework/spring-converters-list/ しかし、それはFileまたはURIをリストしていないので、完全ではないようです。また、私は公式の文書を好むでしょう。

reference documentation chapter on type conversionのいずれも見つかりませんでした。

+1

[DefaultConversionService(https://github.com/spring-projects/spring-framework/blob/b22a59a0c4ea118147dc45c563d68234b8692d97/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService。 java)は、組み込みのコンバータの束を定義します。それはおそらく始めるのに良い場所です。 –

答えて

2

私はそれを把握するために、以下を試してみました:

は私のcontext.xmlに

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/> 

を単一のBeanを定義その後、私は実行します。

public static void main(String[] args) { 
     try (ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml")) { 
      ConversionService service = context.getBean(ConversionService.class); 
      System.out.println(service); 
     } 
} 

そして私は

を取得
ConversionService converters = 
    java.lang.Boolean -> java.lang.String : o[email protected]520a3426 
    java.lang.Character -> java.lang.Number : or[email protected]6b09bb57 
    java.lang.Character -> java.lang.String : o[email protected]5f9d02cb 
    java.lang.Enum -> java.lang.String : [email protected]e9b1010 
    java.lang.Number -> java.lang.Character : org.[email protected]63753b6d 
    java.lang.Number -> java.lang.Number : org.spri[email protected]527740a2 
    java.lang.Number -> java.lang.String : o[email protected]3108bc 
    java.lang.String -> java.lang.Boolean : or[email protected]6536e911 
    java.lang.String -> java.lang.Character : org.[email protected]370736d9 
    java.lang.String -> java.lang.Enum : org.sp[email protected]18eed359 
    java.lang.String -> java.lang.Number : org.spri[email protected]13a5fe33 
    java.lang.String -> java.nio.charset.Charset : or[email protected]185d8b6 
    java.lang.String -> java.util.Currency : org[email protected]335eadca 
    java.lang.String -> java.util.Locale : o[email protected]6c3708b3 
    java.lang.String -> java.util.Properties : org.s[email protected]eec5a4a 
    java.lang.String -> java.util.TimeZone : org[email protected]61f8bee4 
    java.lang.String -> java.util.UUID : [email protected]ddf90b0 
    java.nio.charset.Charset -> java.lang.String : o[email protected]67784306 
    java.time.ZoneId -> java.util.TimeZone : org[email protected]7b49cea0 
    java.time.ZonedDateTime -> java.util.Calendar : org.spring[email protected]887af79 
    java.util.Currency -> java.lang.String : o[email protected]210366b4 
    java.util.Locale -> java.lang.String : o[email protected]6f1fba17 
    java.util.Properties -> java.lang.String : org.s[email protected]2b2948e2 
    java.util.UUID -> java.lang.String : o[email protected]57536d79 
    [email protected]b4523f8 
    org.[email protected]3b0143d3 
    [email protected]7791a895 
    [email protected]1f28c152 
    [email protected]70963 
    [email protected]70963 
    [email protected]70963 
    [email protected]70963 
    org.[email protected]5a8e6209 
    org.sprin[email protected]731a74c 
    org.s[email protected]67b92f0a 
    org.s[email protected]6325a3ee 
    org.sprin[email protected]6e0e048a 
    [email protected]c631b,o[email protected]5b87ed94 
    [email protected]3a2 
    [email protected]3a5ed7a6 
    org.s[email protected]2b9627bc 
    org[email protected]5bc79255 
    [email protected]3 
    org.springframework.core.convert.support.StreamConve[email protected] 
    [email protected]3 
    [email protected]3 
    [email protected]7d907bac 
    org.s[email protected]1d16f93d 

あなたの質問のコメントセクションに記載されているDefaultConversionServiceで提供されているデフォルトのコンバーター。

私は気づいたFallbackObjectToStringConverterかなり何かを文字列に変換します。したがって、URIFileStringは常に動作します。逆の場合は、ObjectToObjectConverterによって処理されているように見えますが、これはコンストラクタを使用してオブジェクトをString表現から戻すのに十分スマートです。

私はあなたが使用しているライブラリとSpringの依存関係によっては、より多くのコンバーターをここに登録している可能性があります。

1

PropertyResolverJava docsをご覧になれば、直接知られているサブインターフェースのリストがあることがわかります。そのようなインターフェースの1つはConfigurablePropertyResolverインターフェースです。 ConfigurablePropertyResolverJavaドキュメントに進むと、ConversionServiceインターフェイスについての説明が表示されます。

ほとんどの環境に適したコンバータと、デフォルト で構成されGenericConversionServiceの専門:あなたはその後、DefaultConversionServiceConversionServiceを実装し、以下ののJavaドキュメントの記述を持っているインターフェースの一つであることがわかります。ダイレクト インスタンシエーション用に設計されていますが、任意のConverterRegistryインスタンスに対して、 addDefaultConverters(ConverterRegistry)ユーティリティメソッドをアドホックで使用します。

デフォルトコンバーターがすべて設定されているようです。 DefaultConversionServicesource-codeを見ると、コンストラクタでaddDefaultConvertersメソッドが呼び出されます。この方法は、デフォルトで利用可能なすべてのコンバータをセットアップするようです。

この回答の目的は1つが、彼らが探している情報を取得するために、ソース・コードの組み合わせで利用できるのJavaドキュメントをナビゲートする方法を実証することでした。

+0

ありがとう、非常に役立つ、私は次の問題のためにそれを念頭に置くでしょう:-) –

関連する問題