Search in sources :

Example 6 with GenericConversionService

use of org.springframework.core.convert.support.GenericConversionService in project spring-framework by spring-projects.

the class DefaultMessageHandlerMethodFactoryTests method customConversion.

@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");
}
Also used : InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 7 with GenericConversionService

use of org.springframework.core.convert.support.GenericConversionService in project spring-data-document-examples by spring-projects.

the class RestaurantController method registerConverters.

@InitBinder
void registerConverters(WebDataBinder binder) {
    if (binder.getConversionService() instanceof GenericConversionService) {
        GenericConversionService conversionService = (GenericConversionService) binder.getConversionService();
        conversionService.addConverter(getRestaurantConverter());
    }
}
Also used : GenericConversionService(org.springframework.core.convert.support.GenericConversionService) InitBinder(org.springframework.web.bind.annotation.InitBinder)

Example 8 with GenericConversionService

use of org.springframework.core.convert.support.GenericConversionService in project spring-data-document-examples by spring-projects.

the class SignUpController method registerConverters.

@InitBinder
void registerConverters(WebDataBinder binder) {
    if (binder.getConversionService() instanceof GenericConversionService) {
        GenericConversionService conversionService = (GenericConversionService) binder.getConversionService();
        conversionService.addConverter(getRestaurantConverter());
        conversionService.addConverter(getUserAccountConverter());
        conversionService.addConverter(getRestaurantConverterFromString());
    }
}
Also used : GenericConversionService(org.springframework.core.convert.support.GenericConversionService) InitBinder(org.springframework.web.bind.annotation.InitBinder)

Example 9 with GenericConversionService

use of org.springframework.core.convert.support.GenericConversionService in project grails-core by grails.

the class AbstractGrailsPluginManager method doRuntimeConfiguration.

/**
     * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
     * @param springConfig The RuntimeSpringConfiguration instance
     */
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {

            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment() && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}
Also used : NavigableMap(org.grails.config.NavigableMap) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) ApplicationContext(org.springframework.context.ApplicationContext) ConverterRegistry(org.springframework.core.convert.converter.ConverterRegistry) GrailsPlugin(grails.plugins.GrailsPlugin) ConversionService(org.springframework.core.convert.ConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) ConfigObject(groovy.util.ConfigObject) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) GenericConversionService(org.springframework.core.convert.support.GenericConversionService)

Example 10 with GenericConversionService

use of org.springframework.core.convert.support.GenericConversionService in project spring-framework by spring-projects.

the class ApplicationContextExpressionTests method prototypeCreationReevaluatesExpressions.

@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
        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");
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Aggregations

GenericConversionService (org.springframework.core.convert.support.GenericConversionService)10 Test (org.junit.Test)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)2 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)2 InvocableHandlerMethod (org.springframework.messaging.handler.invocation.InvocableHandlerMethod)2 InitBinder (org.springframework.web.bind.annotation.InitBinder)2 GrailsPlugin (grails.plugins.GrailsPlugin)1 ConfigObject (groovy.util.ConfigObject)1 Time (java.sql.Time)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 NavigableMap (org.grails.config.NavigableMap)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1