Close

Spring Framework - ConversionService Examples

Spring Framework 

This examples uses DefaultConversionService, which is one of the ConversionService implementations. DefaultConversionService, by default, registers most of the appropriate Converters implementations.

package com.logicbig.example;


import org.springframework.core.convert.support.DefaultConversionService;

import java.util.Collection;
import java.util.Currency;

public class ConversionServiceExample {
public static void main (String[] args) {
DefaultConversionService service = new DefaultConversionService();

Currency convert = service.convert("USD", Currency.class);
System.out.println(convert);

Collection<String> list = service.convert("Deb, Mike, Kim",
Collection.class);
System.out.println(list);
}
}

Output

USD
[Deb, Mike, Kim]




Using ConversionService in Spring container.

package com.logicbig.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ExampleMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);

ClientBean clientBean = context.getBean(ClientBean.class);
clientBean.showAsList("one, two, three");
}
}

Output

one
two
three
package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import java.util.List;

public class ClientBean {
@Autowired
private ConversionService conversionService;


public void showAsList(String listAsString) {
List<String> list = conversionService.convert(listAsString, List.class);
list.forEach(System.out::println);
}
}
package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;

@Configuration
public class AppConfig {
@Bean
public ConversionService conversionService() {
return new DefaultConversionService();
}

@Bean
public ClientBean clientBean() {
return new ClientBean();
}
}
Original Post




This examples uses DefaultConversionService, which is one of the ConversionService implementations. DefaultConversionService, by default, registers most of the appropriate Converters implementations.

package com.logicbig.example;

import org.springframework.core.convert.support.DefaultConversionService;
import java.util.Collection;
import java.util.Currency;

public class ConversionServiceExample {
public static void main (String[] args) {
DefaultConversionService service = new DefaultConversionService();

Currency convert = service.convert("USD", Currency.class);
System.out.println(convert);

Collection<String> list = service.convert("Deb, Mike, Kim",
Collection.class);
System.out.println(list);
}
}

Output

USD
[Deb, Mike, Kim]
Original Post




package com.logicbig.example;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;

public class ConverterFactoryExample {
public static void main(String[] args) {
ConversionService conversionService = new DefaultConversionService();
//StringToNumberConverterFactory is used
Double num = conversionService.convert("1.5", Double.class);
System.out.println(num);
}
}

Output

1.5
Original Post




See Also