当前位置: 首页>>代码示例>>Java>>正文


Java Converter类代码示例

本文整理汇总了Java中org.springframework.core.convert.converter.Converter的典型用法代码示例。如果您正苦于以下问题:Java Converter类的具体用法?Java Converter怎么用?Java Converter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Converter类属于org.springframework.core.convert.converter包,在下文中一共展示了Converter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: commaSeparatedStringToThrowablesCollection

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
@ConfigurationPropertiesBinding
@Bean
public Converter<String, List<Class<? extends Throwable>>> commaSeparatedStringToThrowablesCollection() {
    return new Converter<String, List<Class<? extends Throwable>>>() {
        @Override
        public List<Class<? extends Throwable>> convert(final String source) {
            try {
                final List<Class<? extends Throwable>> classes = new ArrayList<>();
                for (final String className : StringUtils.commaDelimitedListToStringArray(source)) {
                    classes.add((Class<? extends Throwable>) ClassUtils.forName(className.trim(), getClass().getClassLoader()));
                }
                return classes;
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }
    };
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:19,代码来源:CasCoreBootstrapStandaloneConfiguration.java

示例2: addDateConverters

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
private static void addDateConverters(GenericConversionService conversionService) {

		conversionService.addConverter(Date.class, String.class, new Converter<Date, String>() {
			@Override
			public String convert(Date dateVal) {
				return ValueConversionUtil.dateToIsoString(dateVal);
			}
		});
		conversionService.addConverter(String.class, Date.class, blankConverter(new Converter<String, Date>() {
			@Override
			public Date convert(String stringVal) {
				return ValueConversionUtil.isoStringToDate(stringVal);
			}
		}));

	}
 
开发者ID:innodev-au,项目名称:wmboost-data,代码行数:17,代码来源:ConversionServiceUtils.java

示例3: DocumentConversionService

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
public DocumentConversionService(final DocumentFactory docFactory) {
	super();

	internalService.addConverter(IData.class, Document.class, new Converter<IData, Document>() {
		@Override
		public Document convert(IData idata) {
			return docFactory.wrap((IData) idata);
		}
	});
	internalService.addConverter(Document.class, IData.class, new Converter<Document, IData>() {
		@Override
		public IData convert(Document document) {
			return ((Document) document).getIData();
		}
	});
}
 
开发者ID:innodev-au,项目名称:wmboost-data,代码行数:17,代码来源:DocumentConversionService.java

示例4: customConversion

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
@Test
public void customConversion() throws Exception {
	DefaultMessageHandlerMethodFactory instance = createInstance();
	GenericConversionService conversionService = new GenericConversionService();
	conversionService.addConverter(SampleBean.class, String.class, new Converter<SampleBean, String>() {
		@Override
		public String convert(SampleBean source) {
			return "foo bar";
		}
	});
	instance.setConversionService(conversionService);
	instance.afterPropertiesSet();

	InvocableHandlerMethod invocableHandlerMethod =
			createInvocableHandlerMethod(instance, "simpleString", String.class);

	invocableHandlerMethod.invoke(MessageBuilder.withPayload(sample).build());
	assertMethodInvocation(sample, "simpleString");
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:DefaultMessageHandlerMethodFactoryTests.java

示例5: registerConverters

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
/**
 * Register the given Converter objects with the given target ConverterRegistry.
 * @param converters the converter objects: implementing {@link Converter},
 * {@link ConverterFactory}, or {@link GenericConverter}
 * @param registry the target registry
 */
public static void registerConverters(Set<?> converters, ConverterRegistry registry) {
	if (converters != null) {
		for (Object converter : converters) {
			if (converter instanceof GenericConverter) {
				registry.addConverter((GenericConverter) converter);
			}
			else if (converter instanceof Converter<?, ?>) {
				registry.addConverter((Converter<?, ?>) converter);
			}
			else if (converter instanceof ConverterFactory<?, ?>) {
				registry.addConverterFactory((ConverterFactory<?, ?>) converter);
			}
			else {
				throw new IllegalArgumentException("Each converter object must implement one of the " +
						"Converter, ConverterFactory, or GenericConverter interfaces");
			}
		}
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:ConversionServiceFactory.java

示例6: convertFromArrayToStream

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
@Test
@SuppressWarnings("resource")
public void convertFromArrayToStream() throws NoSuchFieldException {
	Integer[] stream = new Integer[] {1, 0, 1};
	this.conversionService.addConverter(new Converter<Integer, Boolean>() {
		@Override
		public Boolean convert(Integer source) {
			return source == 1;
		}
	});
	TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans")); ;
	Object result = this.conversionService.convert(stream, streamOfBoolean);
	assertNotNull("converted object must not be null", result);
	assertTrue("Converted object must be a stream", result instanceof Stream);
	@SuppressWarnings("unchecked")
	Stream<Boolean> content = (Stream<Boolean>) result;
	assertEquals(2, content.filter(x -> x).count());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:StreamConverterTests.java

示例7: testCustomConverter

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
@Test
public void testCustomConverter() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	GenericConversionService conversionService = new DefaultConversionService();
	conversionService.addConverter(new Converter<String, Float>() {
		@Override
		public Float convert(String source) {
			try {
				NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
				return nf.parse(source).floatValue();
			}
			catch (ParseException ex) {
				throw new IllegalArgumentException(ex);
			}
		}
	});
	lbf.setConversionService(conversionService);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:DefaultListableBeanFactoryTests.java

示例8: scan

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
@Override
public Cursor<V> scan(K key, final ScanOptions options) {

	final byte[] rawKey = rawKey(key);
	return execute(new RedisCallback<Cursor<V>>() {

		@Override
		public Cursor<V> doInRedis(RedisConnection connection) throws DataAccessException {
			return new ConvertingCursor<byte[], V>(connection.sScan(rawKey, options), new Converter<byte[], V>() {

				@Override
				public V convert(byte[] source) {
					return deserializeValue(source);
				}
			});
		}
	}, true);

}
 
开发者ID:mauersu,项目名称:redis-admin,代码行数:20,代码来源:DefaultSetOperations.java

示例9: scan

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
@Override
public Cursor<TypedTuple<V>> scan(K key, final ScanOptions options) {

	final byte[] rawKey = rawKey(key);
	Cursor<Tuple> cursor = execute(new RedisCallback<Cursor<Tuple>>() {

		@Override
		public Cursor<Tuple> doInRedis(RedisConnection connection) throws DataAccessException {
			connection.select(dbIndex);
			return connection.zScan(rawKey, options);
		}
	}, true);

	return new ConvertingCursor<Tuple, TypedTuple<V>>(cursor, new Converter<Tuple, TypedTuple<V>>() {

		@Override
		public TypedTuple<V> convert(Tuple source) {
			return deserializeTuple(source);
		}
	});
}
 
开发者ID:mauersu,项目名称:redis-admin,代码行数:22,代码来源:DefaultZSetOperations.java

示例10: conversionService

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
private ConversionService conversionService(ApplicationContext applicationContext) {
	// TODO should be also able to resolve a url to a bean/method
	BeanFactoryResolver beanResolver = new BeanFactoryResolver(applicationContext) {
		@Override
		public Object resolve(EvaluationContext context, String beanName)
				throws AccessException {
			return super.resolve(context,
					(beanName.startsWith("bean:") ? beanName.substring("bean:".length())
							: beanName));
		}
	};

	ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean();
	Set<Converter<?, ?>> cons = new HashSet<Converter<?, ?>>();

	cons.add(new DtoCheckConverter(beanResolver));
	cons.add(new DtoOperationConverter(beanResolver));
	factoryBean.setConverters(cons);
	factoryBean.afterPropertiesSet();

	return factoryBean.getObject();
}
 
开发者ID:cucina,项目名称:opencucina,代码行数:23,代码来源:ClientApplication.java

示例11: registerConvertersIn

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
/**
 * Register custom converters within given {@link org.springframework.core.convert.support.GenericConversionService}
 *
 * @param conversionService must not be null
 */
public void registerConvertersIn(GenericConversionService conversionService) {
	Assert.notNull(conversionService);

	for (Object converter : converters) {
		if (converter instanceof Converter) {
			conversionService.addConverter((Converter<?, ?>) converter);
		} else if (converter instanceof ConverterFactory) {
			conversionService.addConverterFactory((ConverterFactory<?, ?>) converter);
		} else if (converter instanceof GenericConverter) {
			conversionService.addConverter((GenericConverter) converter);
		} else {
			throw new IllegalArgumentException("Given object '" + converter
					+ "' expected to be a Converter, ConverterFactory or GenericeConverter!");
		}
	}
}
 
开发者ID:yiduwangkai,项目名称:dubbox-solr,代码行数:22,代码来源:CustomConversions.java

示例12: registerConversion

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
private void registerConversion(Object converter) {
	Class<?> type = converter.getClass();
	boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
	boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

	if (!isReading && !isWriting) {
		isReading = true;
		isWriting = true;
	}

	if (converter instanceof GenericConverter) {
		GenericConverter genericConverter = (GenericConverter) converter;
		for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
			register(new ConvertibleContext(pair, isReading, isWriting));
		}
	} else if (converter instanceof Converter) {
		Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
		register(new ConvertibleContext(arguments[0], arguments[1], isReading, isWriting));
	} else {
		throw new IllegalArgumentException("Unsupported Converter type! Expected either GenericConverter if Converter.");
	}
}
 
开发者ID:yiduwangkai,项目名称:dubbox-solr,代码行数:23,代码来源:CustomConversions.java

示例13: getConvertersToRegister

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
/**
 * Returns the converters to be registered. Will only return converters in case we're running on Java 8.
 * 
 * @return
 */
public static Collection<Converter<?, ?>> getConvertersToRegister() {

	if (!JAVA_8_IS_PRESENT) {
		return Collections.emptySet();
	}

	List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
	converters.add(DateToLocalDateTimeConverter.INSTANCE);
	converters.add(LocalDateTimeToDateConverter.INSTANCE);
	converters.add(DateToLocalDateConverter.INSTANCE);
	converters.add(LocalDateToDateConverter.INSTANCE);
	converters.add(DateToLocalTimeConverter.INSTANCE);
	converters.add(LocalTimeToDateConverter.INSTANCE);
	converters.add(DateToInstantConverter.INSTANCE);
	converters.add(InstantToDateConverter.INSTANCE);

	return converters;
}
 
开发者ID:mv-informatica,项目名称:components-html5,代码行数:24,代码来源:Jsr310Converters.java

示例14: registerConvertersIn

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
/**
 * Populates the given {@link GenericConversionService} with the converters registered.
 *
 * @param conversionService the service to register.
 */
public void registerConvertersIn(final GenericConversionService conversionService) {
  for (Object converter : converters) {
    boolean added = false;

    if (converter instanceof Converter) {
      conversionService.addConverter((Converter<?, ?>) converter);
      added = true;
    }

    if (converter instanceof ConverterFactory) {
      conversionService.addConverterFactory((ConverterFactory<?, ?>) converter);
      added = true;
    }

    if (converter instanceof GenericConverter) {
      conversionService.addConverter((GenericConverter) converter);
      added = true;
    }

    if (!added) {
      throw new IllegalArgumentException("Given set contains element that is neither Converter nor ConverterFactory!");
    }
  }
}
 
开发者ID:KPTechnologyLab,项目名称:spring-data-crate,代码行数:30,代码来源:CustomConversions.java

示例15: registerConversion

import org.springframework.core.convert.converter.Converter; //导入依赖的package包/类
/**
 * Registers a conversion for the given converter. Inspects either generics or the convertible pairs returned
 * by a {@link GenericConverter}.
 *
 * @param converter the converter to register.
 */
private void registerConversion(final Object converter) {
  Class<?> type = converter.getClass();
  boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
  boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

  if (converter instanceof GenericConverter) {
    GenericConverter genericConverter = (GenericConverter) converter;
    for (GenericConverter.ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
      register(new ConverterRegistration(pair, isReading, isWriting));
    }
  } else if (converter instanceof Converter) {
    Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
    register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));
  } else {
    throw new IllegalArgumentException("Unsupported Converter type!");
  }
}
 
开发者ID:KPTechnologyLab,项目名称:spring-data-crate,代码行数:24,代码来源:CustomConversions.java


注:本文中的org.springframework.core.convert.converter.Converter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。