Search in sources :

Example 31 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-boot by spring-projects.

the class RelaxedDataBinder method modifyProperties.

/**
	 * Modify the property values so that period separated property paths are valid for
	 * map keys. Also creates new maps for properties of map type that are null (assuming
	 * all maps are potentially nested). The standard bracket {@code[...]} dereferencing
	 * is also accepted.
	 * @param propertyValues the property values
	 * @param target the target object
	 * @return modified property values
	 */
private MutablePropertyValues modifyProperties(MutablePropertyValues propertyValues, Object target) {
    propertyValues = getPropertyValuesForNamePrefix(propertyValues);
    if (target instanceof MapHolder) {
        propertyValues = addMapPrefix(propertyValues);
    }
    BeanWrapper wrapper = new BeanWrapperImpl(target);
    wrapper.setConversionService(new RelaxedConversionService(getConversionService()));
    wrapper.setAutoGrowNestedPaths(true);
    List<PropertyValue> sortedValues = new ArrayList<>();
    Set<String> modifiedNames = new HashSet<>();
    List<String> sortedNames = getSortedPropertyNames(propertyValues);
    for (String name : sortedNames) {
        PropertyValue propertyValue = propertyValues.getPropertyValue(name);
        PropertyValue modifiedProperty = modifyProperty(wrapper, propertyValue);
        if (modifiedNames.add(modifiedProperty.getName())) {
            sortedValues.add(modifiedProperty);
        }
    }
    return new MutablePropertyValues(sortedValues);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) HashSet(java.util.HashSet)

Example 32 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method doTestFieldSettingWithInstantiationAwarePostProcessor.

private void doTestFieldSettingWithInstantiationAwarePostProcessor(final boolean skipPropertyPopulation) {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    int ageSetByPropertyValue = 27;
    bd.getPropertyValues().addPropertyValue(new PropertyValue("age", new Integer(ageSetByPropertyValue)));
    lbf.registerBeanDefinition("test", bd);
    final String nameSetOnField = "nameSetOnField";
    lbf.addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {

        @Override
        public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
            TestBean tb = (TestBean) bean;
            try {
                Field f = TestBean.class.getDeclaredField("name");
                f.setAccessible(true);
                f.set(tb, nameSetOnField);
                return !skipPropertyPopulation;
            } catch (Exception ex) {
                fail("Unexpected exception: " + ex);
                // Keep compiler happy about return
                throw new IllegalStateException();
            }
        }
    });
    lbf.preInstantiateSingletons();
    TestBean tb = (TestBean) lbf.getBean("test");
    assertEquals("Name was set on field by IAPP", nameSetOnField, tb.getName());
    if (!skipPropertyPopulation) {
        assertEquals("Property value still set", ageSetByPropertyValue, tb.getAge());
    } else {
        assertEquals("Property value was NOT set and still has default value", 0, tb.getAge());
    }
}
Also used : InstantiationAwareBeanPostProcessorAdapter(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyValue(org.springframework.beans.PropertyValue) ParseException(java.text.ParseException) TypeMismatchException(org.springframework.beans.TypeMismatchException) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) ExpectedException(org.junit.rules.ExpectedException) MalformedURLException(java.net.MalformedURLException) BeansException(org.springframework.beans.BeansException) Field(java.lang.reflect.Field) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeansException(org.springframework.beans.BeansException)

Example 33 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DispatcherServletTests method detectHandlerMappingFromParent.

@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
    // create a parent context that includes a mapping
    StaticWebApplicationContext parent = new StaticWebApplicationContext();
    parent.setServletContext(getServletContext());
    parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
    parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
    parent.refresh();
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    // will have parent
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    ServletConfig config = new MockServletConfig(getServletContext(), "complex");
    config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
    complexDispatcherServlet.init(config);
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(), response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) PropertyValue(org.springframework.beans.PropertyValue) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 34 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testAutowireWithUnsatisfiedConstructorDependency.

@Test
public void testAutowireWithUnsatisfiedConstructorDependency() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("name", "Rod"));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("rod", bd);
    assertEquals(1, lbf.getBeanDefinitionCount());
    try {
        lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
        fail("Should have unsatisfied constructor dependency on SideEffectBean");
    } catch (UnsatisfiedDependencyException ex) {
    // expected
    }
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyValue(org.springframework.beans.PropertyValue) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 35 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class CustomEditorTests method testComplexObject.

@Test
public void testComplexObject() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
    pvs.addPropertyValue(new PropertyValue("name", newName));
    pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
    pvs.addPropertyValue(new PropertyValue("spouse", tbString));
    bw.setPropertyValues(pvs);
    assertTrue("spouse is non-null", tb.getSpouse() != null);
    assertTrue("spouse name is Kerry and age is 34", tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
}
Also used : BigInteger(java.math.BigInteger) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) Test(org.junit.Test)

Aggregations

PropertyValue (org.springframework.beans.PropertyValue)43 Test (org.junit.Test)16 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 FieldAccessBean (org.springframework.tests.sample.beans.FieldAccessBean)6 ArrayList (java.util.ArrayList)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2