本文整理汇总了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);
}
}
};
}
示例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);
}
}));
}
示例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();
}
});
}
示例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");
}
}
}
}
示例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());
}
示例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);
}
示例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);
}
示例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);
}
});
}
示例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();
}
示例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!");
}
}
}
示例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.");
}
}
示例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;
}
示例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!");
}
}
}
示例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!");
}
}