Search in sources :

Example 16 with PropertyValue

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

the class WebDataBinder method checkFieldDefaults.

/**
	 * Check the given property values for field defaults,
	 * i.e. for fields that start with the field default prefix.
	 * <p>The existence of a field defaults indicates that the specified
	 * value should be used if the field is otherwise not present.
	 * @param mpvs the property values to be bound (can be modified)
	 * @see #getFieldDefaultPrefix
	 */
protected void checkFieldDefaults(MutablePropertyValues mpvs) {
    if (getFieldDefaultPrefix() != null) {
        String fieldDefaultPrefix = getFieldDefaultPrefix();
        PropertyValue[] pvArray = mpvs.getPropertyValues();
        for (PropertyValue pv : pvArray) {
            if (pv.getName().startsWith(fieldDefaultPrefix)) {
                String field = pv.getName().substring(fieldDefaultPrefix.length());
                if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
                    mpvs.add(field, pv.getValue());
                }
                mpvs.removePropertyValue(pv);
            }
        }
    }
}
Also used : PropertyValue(org.springframework.beans.PropertyValue)

Example 17 with PropertyValue

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

the class WebRequestDataBinderTests method doTestTony.

/**
	 * Must contain: forname=Tony surname=Blair age=50
	 */
protected void doTestTony(PropertyValues pvs) throws Exception {
    assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
    assertTrue("Contains forname", pvs.contains("forname"));
    assertTrue("Contains surname", pvs.contains("surname"));
    assertTrue("Contains age", pvs.contains("age"));
    assertTrue("Doesn't contain tory", !pvs.contains("tory"));
    PropertyValue[] pvArray = pvs.getPropertyValues();
    Map<String, String> m = new HashMap<>();
    m.put("forname", "Tony");
    m.put("surname", "Blair");
    m.put("age", "50");
    for (PropertyValue pv : pvArray) {
        Object val = m.get(pv.getName());
        assertTrue("Can't have unexpected value", val != null);
        assertTrue("Val i string", val instanceof String);
        assertTrue("val matches expected", val.equals(pv.getValue()));
        m.remove(pv.getName());
    }
    assertTrue("Map size is 0", m.size() == 0);
}
Also used : HashMap(java.util.HashMap) PropertyValue(org.springframework.beans.PropertyValue)

Example 18 with PropertyValue

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

the class ViewResolverTests method testBeanNameViewResolver.

@Test
public void testBeanNameViewResolver() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    MutablePropertyValues pvs1 = new MutablePropertyValues();
    pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
    wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    MutablePropertyValues pvs2 = new MutablePropertyValues();
    pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
    wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(wac);
    wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", InternalResourceView.class, view.getClass());
    assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl());
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 19 with PropertyValue

use of org.springframework.beans.PropertyValue in project grails-core by grails.

the class DefaultRuntimeSpringConfiguration method registerBeanDefinitionsWithRegistry.

private void registerBeanDefinitionsWithRegistry(BeanDefinitionRegistry registry) {
    for (Object key : beanDefinitions.keySet()) {
        BeanDefinition bd = beanDefinitions.get(key);
        if (LOG.isDebugEnabled()) {
            LOG.debug("[RuntimeConfiguration] Registering bean [" + key + "]");
            if (LOG.isTraceEnabled()) {
                PropertyValue[] pvs = bd.getPropertyValues().getPropertyValues();
                for (PropertyValue pv : pvs) {
                    LOG.trace("[RuntimeConfiguration] With property [" + pv.getName() + "] set to [" + pv.getValue() + "]");
                }
            }
        }
        final String beanName = key.toString();
        registry.registerBeanDefinition(beanName, bd);
    }
}
Also used : PropertyValue(org.springframework.beans.PropertyValue) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 20 with PropertyValue

use of org.springframework.beans.PropertyValue in project grails-core by grails.

the class DefaultRuntimeSpringConfiguration method registerBeanConfigsWithRegistry.

private void registerBeanConfigsWithRegistry(BeanDefinitionRegistry registry) {
    for (BeanConfiguration bc : beanConfigs.values()) {
        String beanName = bc.getName();
        if (LOG.isDebugEnabled()) {
            LOG.debug("[RuntimeConfiguration] Registering bean [" + beanName + "]");
            if (LOG.isTraceEnabled()) {
                PropertyValue[] pvs = bc.getBeanDefinition().getPropertyValues().getPropertyValues();
                for (PropertyValue pv : pvs) {
                    LOG.trace("[RuntimeConfiguration] With property [" + pv.getName() + "] set to [" + pv.getValue() + "]");
                }
            }
        }
        registry.registerBeanDefinition(beanName, bc.getBeanDefinition());
    }
}
Also used : PropertyValue(org.springframework.beans.PropertyValue)

Aggregations

PropertyValue (org.springframework.beans.PropertyValue)41 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 Test (org.junit.Test)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 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 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2