2016-04-07 11 views
1

私の春のブートアプリケーションで次の設定を使用してカスタムプロパティエディタを登録しようとしています。次のドキュメントlinkセクション5.4.2.1を参照してください。BeanWrapperで使用するCustomPropertyEditorsの登録

@Bean 
public static CustomEditorConfigurer customEditorConfigurer() { 
    CustomEditorConfigurer configurer = new CustomEditorConfigurer(); 
    configurer.setPropertyEditorRegistrars(new PropertyEditorRegistrar[] { 
       (registry) -> registry.registerCustomEditor(Instant.class, new CustomInstantEditor()) }); 
     return configurer; 
    } 

私はBeanWrapperを作成し、それを使用している場合、私は次のようなエラーに

コード取得しています:

Exception is Failed to convert property value of type [java.lang.String] to required type [java.time.Instant] for property 'chardate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.time.Instant] for property 'chardate': no matching editors or conversion strategy found 

しかし、上記のコードは次の場合に動作します:

BeanWrapper newAccountWrapper = new BeanWrapperImpl(newAccount); 
newAccountWrapper.setPropertyValue("chardate", value); 

をエラーがありますBeanWrapperのCustomEditorを登録します

BeanWrapper newAccountWrapper = new BeanWrapperImpl(newAccount); 
newAccountWrapper.registerCustomEditor(Instant.class, new 
      CustomInstantEditor()); 

CustomEditorConfigurer BeanFactoryPostProcessorを使用してcustomPropertyEditorsを登録できませんか?

追加情報:

BeanWrapper newAccountWrapper = new BeanWrapperImpl(newAccount); 
    newAccountWrapper.registerCustomEditor(Instant.class, new CustomInstantEditor()); 
    newAccountWrapper.registerCustomEditor(Money.class, new CustomMoneyEditor()); 

    newAccountWrapper.setAutoGrowNestedPaths(true); 

    accountDomainElements.forEach((accountElement, value) -> { 
     newAccountWrapper.setPropertyValue(accountElement, value); 
+0

を試してみてください。 Springがインスタンスを生成すると、すべてのコンフィグラーを検出し、それらを作成した 'BeanWrapper'実装に適用します。あなたはそれをしておらず、基本的にこれを迂回しています。なぜ自分のインスタンスを作成するのですか? –

+0

私はBeanWrapperを作成すると、明示的に設定する必要があり、CustomEditorConfigurerは役に立ちません。 –

+0

Spring MVCのものがBeanWrapperインスタンスを作成していくつかの追加設定を行うときはいいえです。これは、 'ConversionService'を設定し、登録された' PropertyEditorRegistrar'インスタンスを使用して、追加のPropertyEditorsを追加します。ただし、インスタンスを自分で作成するときは、このオプションを完全にバイパスします。なぜあなたは自分自身ではない一般的なものであるBeanWrapperの独自のインスタンスを作成する必要があるのですか? –

答えて

0

あなたは `自分をBeanWrapper`し、それに設定さを適用していない作成されていない場合

@Bean 
public CustomEditorConfigurer customEditorConfigurer() { 
    CustomEditorConfigurer configurer = new CustomEditorConfigurer(); 
    Map<Class<?>, Class<? extends PropertyEditor>> customEditors = new HashMap<>(); 
    customEditors.put(Instant.class, CustomInstantEditor.class); 
    configurer.setCustomEditors(customEditors); 
    return configurer; 
} 
関連する問題