本文整理汇总了Java中org.springframework.core.convert.support.GenericConversionService.addConverter方法的典型用法代码示例。如果您正苦于以下问题:Java GenericConversionService.addConverter方法的具体用法?Java GenericConversionService.addConverter怎么用?Java GenericConversionService.addConverter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.core.convert.support.GenericConversionService
的用法示例。
在下文中一共展示了GenericConversionService.addConverter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: safeConversionService
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
private static GenericConversionService safeConversionService() {
final GenericConversionService converter = new GenericConversionService();
converter.addConverter(Object.class, byte[].class, new SerializingConverter());
final DeserializingConverter byteConverter = new DeserializingConverter();
converter.addConverter(byte[].class, Object.class, (byte[] bytes) -> {
try {
return byteConverter.convert(bytes);
} catch (SerializationFailedException e) {
LOG.error("Could not extract attribute: {}", e.getMessage());
return null;
}
});
return converter;
}
示例2: addDateConverters
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的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: customConversion
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的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
示例4: test
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
@Test
@Deprecated
public void test() throws Exception {
AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
ConfigurableWebBindingInitializer binder = new ConfigurableWebBindingInitializer();
GenericConversionService service = new DefaultConversionService();
service.addConverter(new ColorConverter());
binder.setConversionService(service);
adapter.setWebBindingInitializer(binder);
Spr7766Controller controller = new Spr7766Controller();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/colors");
request.setPathInfo("/colors");
request.addParameter("colors", "#ffffff,000000");
MockHttpServletResponse response = new MockHttpServletResponse();
adapter.handle(request, response, controller);
}
示例5: testCustomConverter
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的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);
}
示例6: noDelegateMapConversion
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
@Test
public void noDelegateMapConversion() {
GenericConversionService conversionService = new GenericConversionService();
GenericMapConverter mapConverter = new GenericMapConverter(conversionService);
conversionService.addConverter(mapConverter);
@SuppressWarnings("unchecked")
Map<String, String> result = conversionService.convert("foo = bar, wizz = jogg", Map.class);
assertThat(result, hasEntry("foo", "bar"));
assertThat(result, hasEntry("wizz", "jogg"));
assertThat(
conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.map(Map.class,
TypeDescriptor.valueOf(GenericMapConverterTests.class), TypeDescriptor.valueOf(Integer.class))),
is(false));
}
示例7: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的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!");
}
}
}
示例8: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的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!");
}
}
}
示例9: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
/**
* Register custom converters within given {@link 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!");
}
}
}
示例10: setupProcessor
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
/**
* Sets up the bean post processor and conversion service
*
* @param beans - The bean factory for the the dictionary beans
*/
public static void setupProcessor(DefaultListableBeanFactory beans) {
try {
// UIF post processor that sets component ids
BeanPostProcessor idPostProcessor = ComponentBeanPostProcessor.class.newInstance();
beans.addBeanPostProcessor(idPostProcessor);
beans.setBeanExpressionResolver(new StandardBeanExpressionResolver() {
@Override
protected void customizeEvaluationContext(StandardEvaluationContext evalContext) {
try {
evalContext.registerFunction("getService", ExpressionFunctions.class.getDeclaredMethod("getService", new Class[]{String.class}));
} catch(NoSuchMethodException me) {
LOG.error("Unable to register custom expression to data dictionary bean factory", me);
}
}
});
// special converters for shorthand map and list property syntax
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new StringMapConverter());
conversionService.addConverter(new StringListConverter());
beans.setConversionService(conversionService);
} catch (Exception e1) {
throw new DataDictionaryException("Cannot create component decorator post processor: " + e1.getMessage(),
e1);
}
}
示例11: setupProcessor
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
/**
* Sets up the bean post processor and conversion service
*
* @param beans - The bean factory for the the dictionary beans
*/
public static void setupProcessor(KualiDefaultListableBeanFactory beans) {
try {
// UIF post processor that sets component ids
BeanPostProcessor idPostProcessor = ComponentBeanPostProcessor.class.newInstance();
beans.addBeanPostProcessor(idPostProcessor);
beans.setBeanExpressionResolver(new StandardBeanExpressionResolver());
// special converters for shorthand map and list property syntax
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new StringMapConverter());
conversionService.addConverter(new StringListConverter());
beans.setConversionService(conversionService);
} catch (Exception e1) {
throw new DataDictionaryException("Cannot create component decorator post processor: " + e1.getMessage(),
e1);
}
}
示例12: getConversionService
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
protected GenericConversionService getConversionService(Workbook workbook,
Map<String, Map<String, GenericEntity<Long, ?>>> idsMapping) {
GenericConversionService service = new GenericConversionService();
GenericEntityConverter genericEntityConverter = new GenericEntityConverter(importDataDao, workbook,
new HashMap<Class<?>, Class<?>>(0), idsMapping);
genericEntityConverter.setConversionService(service);
service.addConverter(genericEntityConverter);
service.addConverter(new ProjectConverter());
service.addConverter(new ArtifactConverter());
service.addConverter(new ExternalLinkWrapperConverter());
DefaultConversionService.addDefaultConverters(service);
return service;
}
示例13: setUp
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
@Before
public void setUp() throws NoSuchFieldException {
GenericConversionService genericConversionService = new GenericConversionService();
genericConversionService.addConverter(new Converter<String, Integer>() {
@Override
public Integer convert(String source) {
if (0 == source.length()) {
return null;
}
return NumberUtils.parseNumber(source, Integer.class);
}
});
Mockito.when(conversionServiceFactoryBean.getObject()).thenReturn(genericConversionService);
mockCartEntry();
mockProduct();
}
示例14: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
/**
* Populates the given {@link GenericConversionService} with the convertes registered.
*
* @param conversionService
*/
public void registerConvertersIn(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: testCustomDocFactory
import org.springframework.core.convert.support.GenericConversionService; //导入方法依赖的package包/类
@Test
public void testCustomDocFactory() {
final Values customIData = new Values(new Object[][] {{"key1", "Hello!"}});
GenericConversionService conversionService = new GenericConversionService();
Converter<String, MyClass> converter = new Converter<String, MyClass>() {
public MyClass convert(String source) {
return new MyClass(source);
};
};
conversionService.addConverter(String.class, MyClass.class, converter );
DocumentFactoryBuilder docFactBuilder = new DocumentFactoryBuilder();
docFactBuilder.setConversionService(conversionService);
docFactBuilder.setDirectIDataFactory(new DirectIDataFactory() {
@Override
public IData create() {
return customIData;
}
});
DocumentFactory docFact = docFactBuilder.build();
Document document = docFact.create();
// Confirm custom IData is the one that has been created
assertSame(document.getIData(), customIData);
// Confirm the conversion service we created is used
assertEquals("Hello!", document.entry("key1", MyClass.class).getVal().getText());
}