Example usage for org.springframework.core.convert.support GenericConversionService addConverter

List of usage examples for org.springframework.core.convert.support GenericConversionService addConverter

Introduction

In this page you can find the example usage for org.springframework.core.convert.support GenericConversionService addConverter.

Prototype

@Override
    public <S, T> void addConverter(Class<S> sourceType, Class<T> targetType,
            Converter<? super S, ? extends T> converter) 

Source Link

Usage

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void prototypeCreationReevaluatesExpressions() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
    GenericConversionService cs = new GenericConversionService();
    cs.addConverter(String.class, String.class, new Converter<String, String>() {
        @Override//from  ww w . j  a v  a2s  . co m
        public String convert(String source) {
            return source.trim();
        }
    });
    ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs);
    RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("country", "#{systemProperties.country}");
    rbd.getPropertyValues().add("country2", new TypedStringValue("-#{systemProperties.country}-"));
    ac.registerBeanDefinition("test", rbd);
    ac.refresh();

    try {
        System.getProperties().put("name", "juergen1");
        System.getProperties().put("country", " UK1 ");
        PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test");
        assertEquals("juergen1", tb.getName());
        assertEquals("UK1", tb.getCountry());
        assertEquals("-UK1-", tb.getCountry2());

        System.getProperties().put("name", "juergen2");
        System.getProperties().put("country", "  UK2  ");
        tb = (PrototypeTestBean) ac.getBean("test");
        assertEquals("juergen2", tb.getName());
        assertEquals("UK2", tb.getCountry());
        assertEquals("-UK2-", tb.getCountry2());
    } finally {
        System.getProperties().remove("name");
        System.getProperties().remove("country");
    }
}

From source file:org.springframework.security.acls.jdbc.AclClassIdUtils.java

public AclClassIdUtils() {
    GenericConversionService genericConversionService = new GenericConversionService();
    genericConversionService.addConverter(String.class, Long.class, new StringToLongConverter());
    genericConversionService.addConverter(String.class, UUID.class, new StringToUUIDConverter());
    this.conversionService = genericConversionService;
}

From source file:org.springframework.session.jdbc.JdbcOperationsSessionRepository.java

private static GenericConversionService createDefaultConversionService() {
    GenericConversionService converter = new GenericConversionService();
    converter.addConverter(Object.class, byte[].class, new SerializingConverter());
    converter.addConverter(byte[].class, Object.class, new DeserializingConverter());
    return converter;
}