2016-05-11 4 views
0

私は200のフォーマッタを1つのパッケージに入れているとしましょう。どのように私の代わりにこれを行うのフォーマッタのパッケージを登録することができます。Spring MVCでフォーマッタのパッケージを登録する方法

<mvc:annotation-driven conversion-service="conversionService" /> 

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 
    <property name="formatters"> 
     <set> 
      <ref bean="MyFormatter1" /> 
      <ref bean="MyFormatter2" /> 
      ... 
      <ref bean="MyFormatter200" /> 
     </set> 
    </property> 
</bean> 

答えて

0

私はそのためのJava構成を使用しようとする:

import org.springframework.context.annotation.Configuration; 
import org.springframework.format.Formatter; 
import org.springframework.format.FormatterRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 


@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addFormatters(FormatterRegistry registry) { 
     for (Formatter formatter : collectFormatters()) { 
      registry.addFormatter(formatter); 
     } 
    } 
} 

今、あなたはcollectFormattersメソッドを実装する必要があります。あなたのアプリケーションの詳細がわからないので、それを行う方法は多分異なるでしょう:

1)すべてのフォーマッタに@Component注釈を付けて、このパッケージを自動スキャンに追加すると、Springはそれらを初期化してアプリケーションコンテキスト。その後、あなたはちょうどこれのように注入することができます:@Resource List<Formatter> formatters

2)自分でパッケージを検査するためのロジックを書くことができます(私はSpringがこれをやっている様子を見てみることをお勧めします)。

関連する問題