[セットトップ]Springカスタム属性編集(CustoomEditoConfigrer)とタイプ変換器(Coversion ServiceFactoryBen)が一緒に配置されています。

5744 ワード

問題:
今このような需要がありますが、java.util.Dateタイプのbeanがあります。springのxml設定で初期化したいですが、どうすればいいですか?
ソリューション:
springのプロパティエディタとタイプ変換器はタイプ変換をしていると言ってもいいですが、属性エディタはstringのために他のタイプに回ることが多いです。
方法1:
プロパティエディタを追加:
	<!--        -->
	<bean id="customEditorConfigurer"
		class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean class="com.UtilDatePropertyEditor">
						<property name="formate" value="yyyy-MM-dd HH:mm:ss"></property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
java:
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilDatePropertyEditor extends PropertyEditorSupport {
	private String formate;

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formate);
		try {
			Date date = simpleDateFormat.parse(text);
			this.setValue(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public void setFormate(String formate) {
		this.formate = formate;
	}

}
方法二:
登録タイプ変換器
 Configring a CoversionService
A CoversionService is a stateless offect designed to be instantiated ap plication startup,then shared between multiplication threads.In a Spring appration,you typicalle configure Coversion Service instance.That CoversionService will be picked up by Spring and then used whenever a type conversion needs to be performed by the framwork.You may also inject this Coversion Service into any of your bens and invorect.
ノート
IfのCoversionService is registed with Spring、the origginal PropertyEditor-based system is used.
To register a default CoversionService with Spring,add the follwing bean definition with id  conversionService
<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean"/>
      
A default CoversionService can convert between stings、numbers、enums、collection、maps、and other common types、To suppliment or override the default converter s with your own custom converter、set the  converters property.Property values may implement either of the Conter,CoverterFactory,or GeneraicConter interfaces.
<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="example.MyCustomConverter"/>
        </list>
    </property>
</bean>
以上がspringオフィシャルreferenceの登録タイプコンバータについて紹介しています。
o suppliment or override the default converter s with your own custom converter(s)、set the  converters property.Property values may implement either of the Conter,CoverterFactory,or GeneraicConter interfaces."
可能性を教えてください。 Coverter、CoverterFactory、or GeneraicConverterの3つのインターフェースのうちの1つと対応する構成がタイプ変換を補足します。
	<bean id="conversionService"
		class="org.springframework.context.support.ConversionServiceFactoryBean">
		<property name="converters">
			<list>
				<bean class="String2DateConverter" />
			</list>
		</property>
	</bean>
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

public class String2DateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String arg0) {
		try {
			return DateUtils.parseDate(arg0,
					new String[] { "yyyy-MM-dd HH:mm:ss" });
		} catch (ParseException e) {
			return null;
		}
	}

}
java.lang.IllagalAgment Exception:Left-hand side type must not be null異常以上の二つの方法は需要を満たすことができますが、一緒に使うことができないとこの問題が発生します。これはspringの一つのバグです。その中の一つを選ばなければならないです。
バグの説明: https://jira.springsource.org/browse/SPR-6807  その中で、 CoversionService fails with Customome Editon figur.While it is not recommanded to mix the same context it is unavoble
spring関連アプリ: http://static.springsource.org/spring/docs/3.0.x/reference/validation.html