Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#setConversionService()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#setConversionService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void optionalPropertyWithValue() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setConversionService(new DefaultConversionService());
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(OptionalTestBean.class)
					.addPropertyValue("name", "${my.name}")
					.getBeanDefinition());

	MockEnvironment env = new MockEnvironment();
	env.setProperty("my.name", "myValue");

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setEnvironment(env);
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue")));
}
 
Example 2
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void optionalPropertyWithoutValue() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setConversionService(new DefaultConversionService());
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(OptionalTestBean.class)
					.addPropertyValue("name", "${my.name}")
					.getBeanDefinition());

	MockEnvironment env = new MockEnvironment();
	env.setProperty("my.name", "");

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setEnvironment(env);
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.setNullValue("");
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.empty()));
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 4
Source File: PropertySourcesPlaceholderConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void optionalPropertyWithValue() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setConversionService(new DefaultConversionService());
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(OptionalTestBean.class)
					.addPropertyValue("name", "${my.name}")
					.getBeanDefinition());

	MockEnvironment env = new MockEnvironment();
	env.setProperty("my.name", "myValue");

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setEnvironment(env);
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue")));
}
 
Example 5
Source File: PropertySourcesPlaceholderConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void optionalPropertyWithoutValue() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setConversionService(new DefaultConversionService());
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(OptionalTestBean.class)
					.addPropertyValue("name", "${my.name}")
					.getBeanDefinition());

	MockEnvironment env = new MockEnvironment();
	env.setProperty("my.name", "");

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setEnvironment(env);
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.setNullValue("");
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.empty()));
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 8
Source File: ConversionServiceResolverTest.java    From spring-context-support with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFromBeanFactory() {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

    ConversionService conversionService = new DefaultConversionService();

    beanFactory.setConversionService(conversionService);

    ConversionServiceResolver resolver = new ConversionServiceResolver(beanFactory);

    assertEquals(conversionService, resolver.resolve(false));
    assertEquals(conversionService, resolver.resolve(true));
    assertEquals(conversionService, resolver.resolve());
}
 
Example 9
Source File: DataDictionary.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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);
    }
}