Java 类org.springframework.core.convert.support.DefaultConversionService 实例源码

项目:mango-spring-boot-starter    文件:MangoConfigFactory.java   
public static MangoConfig getMangoConfig(DefaultListableBeanFactory beanFactory,String prefix){
    MangoConfig target = new MangoConfig();
    PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(target);
    factory.setPropertySources(deducePropertySources(beanFactory));
    factory.setConversionService(new DefaultConversionService());
    factory.setIgnoreInvalidFields(false);
    factory.setIgnoreUnknownFields(true);
    factory.setIgnoreNestedProperties(false);
    factory.setTargetName(prefix);
    try {
        factory.bindPropertiesToTarget();
    }
    catch (Exception ex) {
        throw new MangoAutoConfigException(ex);
    }
    return target;
}
项目:spring4-understanding    文件:HeaderMethodArgumentResolverTests.java   
@Before
public void setup() throws Exception {
    @SuppressWarnings("resource")
    GenericApplicationContext cxt = new GenericApplicationContext();
    cxt.refresh();
    this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());

    Method method = getClass().getDeclaredMethod("handleMessage",
            String.class, String.class, String.class, String.class, String.class);
    this.paramRequired = new SynthesizingMethodParameter(method, 0);
    this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1);
    this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
    this.paramNotAnnotated = new SynthesizingMethodParameter(method, 3);
    this.paramNativeHeader = new SynthesizingMethodParameter(method, 4);

    this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
    GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class);
}
项目:spring4-understanding    文件:DataBinderTests.java   
@Test
public void testBindingErrorWithFormatter() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1x2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(0.0), tb.getMyFloat());
        assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
        assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:spring4-understanding    文件:DataBinderTests.java   
@Test
public void testBindingWithFormatterAgainstList() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("integerList[0]", "1");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Integer(1), tb.getIntegerList().get(0));
        assertEquals("1", binder.getBindingResult().getFieldValue("integerList[0]"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:spring4-understanding    文件:DataBinderTests.java   
@Test
public void testBindingErrorWithFormatterAgainstList() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("integerList[0]", "1x2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertTrue(tb.getIntegerList().isEmpty());
        assertEquals("1x2", binder.getBindingResult().getFieldValue("integerList[0]"));
        assertTrue(binder.getBindingResult().hasFieldErrors("integerList[0]"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:spring4-understanding    文件:DataBinderTests.java   
@Test
public void testBindingErrorWithFormatterAgainstFields() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    binder.initDirectFieldAccess();
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1x2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(0.0), tb.getMyFloat());
        assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
        assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:spring4-understanding    文件:NumberFormattingTests.java   
@Before
public void setUp() {
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.setEmbeddedValueResolver(new StringValueResolver() {
        @Override
        public String resolveStringValue(String strVal) {
            if ("${pattern}".equals(strVal)) {
                return "#,##.00";
            }
            else {
                return strVal;
            }
        }
    });
    conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    LocaleContextHolder.setLocale(Locale.US);
    binder = new DataBinder(new TestBean());
    binder.setConversionService(conversionService);
}
项目:spring4-understanding    文件:Spr7766Tests.java   
@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);
}
项目:spring4-understanding    文件:ServletModelAttributeMethodProcessorTests.java   
@Before
public void setUp() throws Exception {
    this.processor = new ServletModelAttributeMethodProcessor(false);

    Method method = getClass().getDeclaredMethod("modelAttribute",
            TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);

    this.testBeanModelAttr = new MethodParameter(method, 0);
    this.testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
    this.testBeanWithOptionalModelAttr = new MethodParameter(method, 2);

    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());

    this.binderFactory = new ServletRequestDataBinderFactory(null, initializer);
    this.mavContainer = new ModelAndViewContainer();

    this.request = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(request);
}
项目:spring4-understanding    文件:RequestParamMethodArgumentResolverTests.java   
@Test
@SuppressWarnings("rawtypes")
public void resolveOptional() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

    Object result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory);
    assertEquals(Optional.class, result.getClass());
    assertEquals(Optional.empty(), result);

    this.request.addParameter("name", "123");
    result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory);
    assertEquals(Optional.class, result.getClass());
    assertEquals(123, ((Optional) result).get());
}
项目:spring4-understanding    文件:DefaultListableBeanFactoryTests.java   
@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);
}
项目:spring-data-keyvalue-redis    文件:MappingRedisConverter.java   
public MappingRedisConverter(IndexConfiguration indexConfiguration, ReferenceResolver referenceResolver) {

        mappingContext = new KeyValueMappingContext();
        entityInstantiators = new EntityInstantiators();

        this.conversionService = new DefaultConversionService();
        this.conversionService.addConverter(new BinaryConverters.StringToBytesConverter());
        this.conversionService.addConverter(new BinaryConverters.BytesToStringConverter());
        this.conversionService.addConverter(new BinaryConverters.NumberToBytesConverter());
        this.conversionService.addConverterFactory(new BinaryConverters.BytesToNumberConverterFactory());
        this.conversionService.addConverter(new BinaryConverters.EnumToBytesConverter());
        this.conversionService.addConverterFactory(new BinaryConverters.BytesToEnumConverterFactory());
        this.conversionService.addConverter(new BinaryConverters.BooleanToBytesConverter());
        this.conversionService.addConverter(new BinaryConverters.BytesToBooleanConverter());
        this.conversionService.addConverter(new BinaryConverters.DateToBytesConverter());
        this.conversionService.addConverter(new BinaryConverters.BytesToDateConverter());

        typeMapper = new DefaultTypeMapper<RedisData>(new RedisTypeAliasAccessor(this.conversionService));

        this.indexConfiguration = indexConfiguration != null ? indexConfiguration : new IndexConfiguration();
        this.referenceResolver = referenceResolver;
    }
项目:contestparser    文件:ShellPropertiesTests.java   
@Test
public void testBindingSsh() {
    ShellProperties props = new ShellProperties();
    RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
    binder.setConversionService(new DefaultConversionService());
    Map<String, String> map = new HashMap<String, String>();
    map.put("shell.ssh.enabled", "true");
    map.put("shell.ssh.port", "2222");
    map.put("shell.ssh.key_path", "~/.ssh/test.pem");
    binder.bind(new MutablePropertyValues(map));
    assertFalse(binder.getBindingResult().hasErrors());

    Properties p = props.asCrshShellConfig();

    assertEquals("2222", p.get("crash.ssh.port"));
    assertEquals("~/.ssh/test.pem", p.get("crash.ssh.keypath"));
}
项目:contestparser    文件:ShellPropertiesTests.java   
@Test
public void testBindingSshIgnored() {
    ShellProperties props = new ShellProperties();
    RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
    binder.setConversionService(new DefaultConversionService());
    Map<String, String> map = new HashMap<String, String>();
    map.put("shell.ssh.enabled", "false");
    map.put("shell.ssh.port", "2222");
    map.put("shell.ssh.key_path", "~/.ssh/test.pem");
    binder.bind(new MutablePropertyValues(map));
    assertFalse(binder.getBindingResult().hasErrors());

    Properties p = props.asCrshShellConfig();

    assertNull(p.get("crash.ssh.port"));
    assertNull(p.get("crash.ssh.keypath"));
}
项目:contestparser    文件:ShellPropertiesTests.java   
@Test
public void testBindingSimple() {
    SimpleAuthenticationProperties props = new SimpleAuthenticationProperties();
    RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.simple");
    binder.setConversionService(new DefaultConversionService());
    Map<String, String> map = new HashMap<String, String>();
    map.put("shell.auth.simple.user.name", "username123");
    map.put("shell.auth.simple.user.password", "password123");
    binder.bind(new MutablePropertyValues(map));
    assertFalse(binder.getBindingResult().hasErrors());

    Properties p = new Properties();
    props.applyToCrshShellConfig(p);

    assertEquals("username123", p.get("crash.auth.simple.username"));
    assertEquals("password123", p.get("crash.auth.simple.password"));
}
项目:spring-cloud-stream    文件:AbstractBinderTests.java   
protected DirectChannel createBindableChannel(String channelName,
        BindingProperties bindingProperties, boolean inputChannel) throws Exception {
    BindingServiceProperties bindingServiceProperties = new BindingServiceProperties();
    bindingServiceProperties.getBindings().put(channelName, bindingProperties);
    ConfigurableApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.refresh();
    bindingServiceProperties.setApplicationContext(applicationContext);
    bindingServiceProperties.setConversionService(new DefaultConversionService());
    bindingServiceProperties.afterPropertiesSet();
    DirectChannel channel = new DirectChannel();
    channel.setBeanName(channelName);
    MessageConverterConfigurer messageConverterConfigurer = new MessageConverterConfigurer(
            bindingServiceProperties,
            new CompositeMessageConverterFactory(null, null));
    messageConverterConfigurer.setBeanFactory(applicationContext.getBeanFactory());
    if (inputChannel) {
        messageConverterConfigurer.configureInputChannel(channel, channelName);
    }
    else {
        messageConverterConfigurer.configureOutputChannel(channel, channelName);
    }
    return channel;
}
项目:class-guard    文件:DataBinderTests.java   
public void testBindingErrorWithFormatter() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1x2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(0.0), tb.getMyFloat());
        assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
        assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:class-guard    文件:DataBinderTests.java   
public void testBindingWithFormatterAgainstList() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("integerList[0]", "1");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Integer(1), tb.getIntegerList().get(0));
        assertEquals("1", binder.getBindingResult().getFieldValue("integerList[0]"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:class-guard    文件:DataBinderTests.java   
public void testBindingErrorWithFormatterAgainstList() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("integerList[0]", "1x2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertTrue(tb.getIntegerList().isEmpty());
        assertEquals("1x2", binder.getBindingResult().getFieldValue("integerList[0]"));
        assertTrue(binder.getBindingResult().hasFieldErrors("integerList[0]"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:class-guard    文件:DataBinderTests.java   
public void testBindingErrorWithFormatterAgainstFields() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    binder.initDirectFieldAccess();
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1x2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(0.0), tb.getMyFloat());
        assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
        assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:class-guard    文件:NumberFormattingTests.java   
@Before
public void setUp() {
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.setEmbeddedValueResolver(new StringValueResolver() {
        @Override
        public String resolveStringValue(String strVal) {
            if ("${pattern}".equals(strVal)) {
                return "#,##.00";
            } else {
                return strVal;
            }
        }
    });
    conversionService.addFormatterForFieldType(Number.class, new NumberFormatter());
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    LocaleContextHolder.setLocale(Locale.US);
    binder = new DataBinder(new TestBean());
    binder.setConversionService(conversionService);
}
项目:class-guard    文件:JodaTimeFormattingTests.java   
private void setUp(JodaTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);

    registrar.registerFormatters(conversionService);

    JodaTimeBean bean = new JodaTimeBean();
    bean.getChildren().add(new JodaTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
    JodaTimeContext context = new JodaTimeContext();
    context.setTimeZone(DateTimeZone.forID("-05:00"));
    JodaTimeContextHolder.setJodaTimeContext(context);
}
项目:class-guard    文件:Spr7766Tests.java   
@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);
}
项目:class-guard    文件:SerlvetModelAttributeMethodProcessorTests.java   
@Before
public void setUp() throws Exception {
    this.processor = new ServletModelAttributeMethodProcessor(false);

    Method method = getClass().getDeclaredMethod("modelAttribute",
            TestBean.class, TestBeanWithoutStringConstructor.class);

    this.testBeanModelAttr = new MethodParameter(method, 0);
    this.testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);

    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());

    this.binderFactory = new ServletRequestDataBinderFactory(null, initializer );
    this.mavContainer = new ModelAndViewContainer();

    this.request = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(request);
}
项目:class-guard    文件:DefaultListableBeanFactoryTests.java   
@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);
}
项目:artifact-listener    文件:ProjectImportDataServiceImpl.java   
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;
}
项目:cas-5.1.0    文件:CasCoreUtilConfiguration.java   
@PostConstruct
public void init() {
    final ConfigurableApplicationContext ctx = applicationContextProvider().getConfigurableApplicationContext();
    final DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(true);
    conversionService.setEmbeddedValueResolver(new CasConfigurationEmbeddedValueResolver(ctx));
    ctx.getEnvironment().setConversionService(conversionService);
    final ConfigurableEnvironment env = (ConfigurableEnvironment) ctx.getParent().getEnvironment();
    env.setConversionService(conversionService);
    final ConverterRegistry registry = (ConverterRegistry) DefaultConversionService.getSharedInstance();
    registry.addConverter(zonedDateTimeToStringConverter());
}
项目:configx    文件:ConfigPropertyResolver.java   
/**
 * Initialize default ConversionService
 *
 * @return
 */
private ConfigurableConversionService getDefaultConversionService() {
    if (this.defaultConversionService == null) {
        DefaultConversionService conversionService = new DefaultConversionService();
        this.applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
        for (Converter<?, ?> converter : this.converters) {
            conversionService.addConverter(converter);
        }
        for (GenericConverter genericConverter : this.genericConverters) {
            conversionService.addConverter(genericConverter);
        }
        this.defaultConversionService = conversionService;
    }
    return this.defaultConversionService;
}
项目:spring4-understanding    文件:DestinationVariableMethodArgumentResolverTests.java   
@Before
public void setup() throws Exception {
    this.resolver = new DestinationVariableMethodArgumentResolver(new DefaultConversionService());

    Method method = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class);
    this.paramAnnotated = new MethodParameter(method, 0);
    this.paramAnnotatedValue = new MethodParameter(method, 1);
    this.paramNotAnnotated = new MethodParameter(method, 2);

    this.paramAnnotated.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
    GenericTypeResolver.resolveParameterType(this.paramAnnotated, DestinationVariableMethodArgumentResolver.class);
    this.paramAnnotatedValue.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
    GenericTypeResolver.resolveParameterType(this.paramAnnotatedValue, DestinationVariableMethodArgumentResolver.class);
}
项目:spring4-understanding    文件:DataBinderTests.java   
@Test
public void testBindingWithFormatter() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(1.2), tb.getMyFloat());
        assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat"));

        PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
        assertNotNull(editor);
        editor.setValue(new Float(1.4));
        assertEquals("1,4", editor.getAsText());

        editor = binder.getBindingResult().findEditor("myFloat", null);
        assertNotNull(editor);
        editor.setAsText("1,6");
        assertEquals(new Float(1.6), editor.getValue());
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:spring4-understanding    文件:DataBinderTests.java   
@Test
public void testBindingWithFormatterAgainstFields() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    binder.initDirectFieldAccess();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,2");

    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(1.2), tb.getMyFloat());
        assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat"));

        PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
        assertNotNull(editor);
        editor.setValue(new Float(1.4));
        assertEquals("1,4", editor.getAsText());

        editor = binder.getBindingResult().findEditor("myFloat", null);
        assertNotNull(editor);
        editor.setAsText("1,6");
        assertEquals(new Float(1.6), editor.getValue());
    }
    finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
项目:spring4-understanding    文件:JodaTimeFormattingTests.java   
private void setUp(JodaTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);

    JodaTimeBean bean = new JodaTimeBean();
    bean.getChildren().add(new JodaTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
    JodaTimeContext context = new JodaTimeContext();
    context.setTimeZone(DateTimeZone.forID("-05:00"));
    JodaTimeContextHolder.setJodaTimeContext(context);
}
项目:spring4-understanding    文件:DateTimeFormattingTests.java   
private void setUp(DateTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);

    DateTimeBean bean = new DateTimeBean();
    bean.getChildren().add(new DateTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
    DateTimeContext context = new DateTimeContext();
    context.setTimeZone(ZoneId.of("-05:00"));
    DateTimeContextHolder.setDateTimeContext(context);
}
项目:spring4-understanding    文件:DateFormattingTests.java   
private void setUp(DateFormatterRegistrar registrar) {
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);

    SimpleDateBean bean = new SimpleDateBean();
    bean.getChildren().add(new SimpleDateBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
}
项目:spring4-understanding    文件:MimeTypeTests.java   
@Test
public void withConversionService() {
    ConversionService conversionService = new DefaultConversionService();
    assertTrue(conversionService.canConvert(String.class, MimeType.class));
    MimeType mimeType = MimeType.valueOf("application/xml");
    assertEquals(mimeType, conversionService.convert("application/xml", MimeType.class));
}
项目:spring4-understanding    文件:Spr7839Tests.java   
@Before
public void setUp() {
    ConfigurableWebBindingInitializer binder = new ConfigurableWebBindingInitializer();
    GenericConversionService service = new DefaultConversionService();
    service.addConverter(new Converter<String, NestedBean>() {
        @Override
        public NestedBean convert(String source) {
            return new NestedBean(source);
        }
    });
    binder.setConversionService(service);
    adapter.setWebBindingInitializer(binder);
}
项目:spring4-understanding    文件:ConvertingEncoderDecoderSupportTests.java   
@Bean
public ConversionService webSocketConversionService() {
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new MyTypeToStringConverter());
    conversionService.addConverter(new MyTypeToBytesConverter());
    conversionService.addConverter(new StringToMyTypeConverter());
    conversionService.addConverter(new BytesToMyTypeConverter());
    return conversionService;
}
项目:spring4-understanding    文件:ObjectToStringHttpMessageConverterTests.java   
@Before
public void setUp() {
    ConversionService conversionService = new DefaultConversionService();
    this.converter = new ObjectToStringHttpMessageConverter(conversionService);

    this.servletResponse = new MockHttpServletResponse();
    this.response = new ServletServerHttpResponse(this.servletResponse);
}
项目:spring4-understanding    文件:ObjectToStringHttpMessageConverterTests.java   
@Test
public void defaultCharsetModified() throws IOException {
    Charset charset = Charset.forName("UTF-16");
    ConversionService cs = new DefaultConversionService();
    ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, charset);
    converter.write((byte) 31, null, this.response);

    assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());
}
项目:spring4-understanding    文件:MediaTypeTests.java   
@Test
public void testWithConversionService() {
    ConversionService conversionService = new DefaultConversionService();
    assertTrue(conversionService.canConvert(String.class, MediaType.class));
    MediaType mediaType = MediaType.parseMediaType("application/xml");
    assertEquals(mediaType, conversionService.convert("application/xml", MediaType.class));
}