Java Code Examples for org.springframework.core.convert.support.DefaultConversionService#getSharedInstance()

The following examples show how to use org.springframework.core.convert.support.DefaultConversionService#getSharedInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractPropertyResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Convert the given value to the specified target type, if necessary.
 * @param value the original property value
 * @param targetType the specified target type for property retrieval
 * @return the converted value, or the original value if no conversion
 * is necessary
 * @since 4.3.5
 */
@SuppressWarnings("unchecked")
@Nullable
protected <T> T convertValueIfNecessary(Object value, @Nullable Class<T> targetType) {
	if (targetType == null) {
		return (T) value;
	}
	ConversionService conversionServiceToUse = this.conversionService;
	if (conversionServiceToUse == null) {
		// Avoid initialization of shared DefaultConversionService if
		// no standard type conversion is needed in the first place...
		if (ClassUtils.isAssignableValue(targetType, value)) {
			return (T) value;
		}
		conversionServiceToUse = DefaultConversionService.getSharedInstance();
	}
	return conversionServiceToUse.convert(value, targetType);
}
 
Example 2
Source File: AbstractPropertyResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Convert the given value to the specified target type, if necessary.
 * @param value the original property value
 * @param targetType the specified target type for property retrieval
 * @return the converted value, or the original value if no conversion
 * is necessary
 * @since 4.3.5
 */
@SuppressWarnings("unchecked")
@Nullable
protected <T> T convertValueIfNecessary(Object value, @Nullable Class<T> targetType) {
	if (targetType == null) {
		return (T) value;
	}
	ConversionService conversionServiceToUse = this.conversionService;
	if (conversionServiceToUse == null) {
		// Avoid initialization of shared DefaultConversionService if
		// no standard type conversion is needed in the first place...
		if (ClassUtils.isAssignableValue(targetType, value)) {
			return (T) value;
		}
		conversionServiceToUse = DefaultConversionService.getSharedInstance();
	}
	return conversionServiceToUse.convert(value, targetType);
}
 
Example 3
Source File: AbstractPropertyResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert the given value to the specified target type, if necessary.
 * @param value the original property value
 * @param targetType the specified target type for property retrieval
 * @return the converted value, or the original value if no conversion
 * is necessary
 * @since 4.3.5
 */
@SuppressWarnings("unchecked")
protected <T> T convertValueIfNecessary(Object value, Class<T> targetType) {
	if (targetType == null) {
		return (T) value;
	}
	ConversionService conversionServiceToUse = this.conversionService;
	if (conversionServiceToUse == null) {
		// Avoid initialization of shared DefaultConversionService if
		// no standard type conversion is needed in the first place...
		if (ClassUtils.isAssignableValue(targetType, value)) {
			return (T) value;
		}
		conversionServiceToUse = DefaultConversionService.getSharedInstance();
	}
	return conversionServiceToUse.convert(value, targetType);
}
 
Example 4
Source File: AbstractEndpointTests.java    From quickfixj-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private void load(Function<EndpointId, Long> timeToLive,
		PathMapper endpointPathMapper,
		Class<?> configuration,
		Consumer<WebEndpointDiscoverer> consumer) {

	try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configuration)) {
		ConversionServiceParameterValueMapper parameterMapper = new ConversionServiceParameterValueMapper(DefaultConversionService.getSharedInstance());
		EndpointMediaTypes mediaTypes = new EndpointMediaTypes(
				Collections.singletonList("application/json"),
				Collections.singletonList("application/json"));

		WebEndpointDiscoverer discoverer = new WebEndpointDiscoverer(context,
				parameterMapper,
				mediaTypes,
				Collections.singletonList(endpointPathMapper),
				Collections.singleton(new CachingOperationInvokerAdvisor(timeToLive)),
				Collections.emptyList());

		consumer.accept(discoverer);
	}
}
 
Example 5
Source File: GrayDecisionFactoryConfiguration.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
/**
     * 不可引入spring mvc中的ConversionService, 否则会导致feign 加载时,找不到ServletContext, 从而出现异常:No ServletContext set
     *
     * @param validator         校验器
     * @param decisionFactories 灰度决策工厂类列表
     * @return 灰度决策工厂管理器
     */
    @Bean
    @ConditionalOnMissingBean
    public GrayDecisionFactoryKeeper grayDecisionFactoryKeeper(
            /*List<ConversionService> conversionServices, */
            Validator validator, List<GrayDecisionFactory> decisionFactories) {
//        if (CollectionUtils.isNotEmpty(conversionServices)) {
//            return new DefaultGrayDecisionFactoryKeeper(conversionServices.get(0), validator, decisionFactories);
//        }
        return new DefaultGrayDecisionFactoryKeeper(DefaultConversionService.getSharedInstance(), validator, decisionFactories);

    }
 
Example 6
Source File: GenericMessageConverter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Create a new instance with a default {@link ConversionService}.
 */
public GenericMessageConverter() {
	this.conversionService = DefaultConversionService.getSharedInstance();
}
 
Example 7
Source File: GenericMessageConverter.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new instance with a default {@link ConversionService}.
 */
public GenericMessageConverter() {
	this.conversionService = DefaultConversionService.getSharedInstance();
}
 
Example 8
Source File: RedisKeySerializer.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
public RedisKeySerializer(Charset charset) {
	Objects.requireNonNull(charset, "Charset must not be null");
	this.charset = charset;
	this.converter = DefaultConversionService.getSharedInstance();
}
 
Example 9
Source File: StandardTypeConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a StandardTypeConverter for the default ConversionService.
 */
public StandardTypeConverter() {
	this.conversionService = DefaultConversionService.getSharedInstance();
}
 
Example 10
Source File: AbstractNamedValueMethodArgumentResolver.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Constructor with a {@link ConversionService} and a {@link BeanFactory}.
 * @param cs conversion service for converting values to match the
 * target method parameter type
 * @param beanFactory a bean factory to use for resolving {@code ${...}} placeholder
 * and {@code #{...}} SpEL expressions in default values, or {@code null} if default
 * values are not expected to contain expressions
 */
protected AbstractNamedValueMethodArgumentResolver(ConversionService cs,
		@Nullable ConfigurableBeanFactory beanFactory) {

	this.conversionService = (cs != null ? cs : DefaultConversionService.getSharedInstance());
	this.configurableBeanFactory = beanFactory;
	this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
}
 
Example 11
Source File: StandardTypeConverter.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a StandardTypeConverter for the default ConversionService.
 * @see DefaultConversionService#getSharedInstance()
 */
public StandardTypeConverter() {
	this.conversionService = DefaultConversionService.getSharedInstance();
}
 
Example 12
Source File: StandardTypeConverter.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Create a StandardTypeConverter for the default ConversionService.
 * @see DefaultConversionService#getSharedInstance()
 */
public StandardTypeConverter() {
	this.conversionService = DefaultConversionService.getSharedInstance();
}
 
Example 13
Source File: MyResultProcessor.java    From specification-with-projection with MIT License 2 votes vote down vote up
/**
 * Creates a new {@link ProjectingConverter} for the given {@link ReturnedType} and {@link ProjectionFactory}.
 *
 * @param type must not be {@literal null}.
 * @param factory must not be {@literal null}.
 */
ProjectingConverter(ReturnedType type, ProjectionFactory factory) {
    this(type, factory, DefaultConversionService.getSharedInstance());
}