org.macrocloud.kernel.toolkit.convert.BaseConversionService Maven / Gradle / Ivy
package org.macrocloud.kernel.toolkit.convert;
import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.StringValueResolver;
/**
* 类型 转换 服务,添加了 IEnum 转换.
*
* @author macro
*/
public class BaseConversionService extends ApplicationConversionService {
/** The shared instance. */
@Nullable
private static volatile BaseConversionService SHARED_INSTANCE;
/**
* Instantiates a new base conversion service.
*/
public BaseConversionService() {
this(null);
}
/**
* Instantiates a new base conversion service.
*
* @param embeddedValueResolver the embedded value resolver
*/
public BaseConversionService(@Nullable StringValueResolver embeddedValueResolver) {
super(embeddedValueResolver);
super.addConverter(new EnumToStringConverter());
super.addConverter(new StringToEnumConverter());
}
/**
* Return a shared default application {@code ConversionService} instance, lazily
* building it once needed.
*
* Note: This method actually returns an {@link BaseConversionService}
* instance. However, the {@code ConversionService} signature has been preserved for
* binary compatibility.
* @return the shared {@code MacroConversionService} instance (never{@code null})
*/
public static GenericConversionService getInstance() {
BaseConversionService sharedInstance = BaseConversionService.SHARED_INSTANCE;
if (sharedInstance == null) {
synchronized (BaseConversionService.class) {
sharedInstance = BaseConversionService.SHARED_INSTANCE;
if (sharedInstance == null) {
sharedInstance = new BaseConversionService();
BaseConversionService.SHARED_INSTANCE = sharedInstance;
}
}
}
return sharedInstance;
}
}