@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@Test public void bindingNoErrorsNotIgnoreUnknown() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); pvs.addPropertyValue(new PropertyValue("age", new Integer(32))); pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue")); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
/** * Create a binder for the target object. The binder will then be used to * bind the properties form a field set into the target object. This * implementation creates a new {@link DataBinder} and calls out to * {@link #initBinder(DataBinder)} and * {@link #registerCustomEditors(PropertyEditorRegistry)}. * * @param target Object to bind to * @return a {@link DataBinder} that can be used to bind properties to the * target. */ protected DataBinder createBinder(Object target) { DataBinder binder = new DataBinder(target); binder.setIgnoreUnknownFields(!this.strict); initBinder(binder); registerCustomEditors(binder); if(this.conversionService != null) { binder.setConversionService(this.conversionService); } return binder; }
@Test public void testBindingWithSystemFieldError() { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("class.classLoader.URLs[0]", "http://myserver"); binder.setIgnoreUnknownFields(false); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { assertTrue(ex.getMessage().contains("classLoader")); } }
@Test public void testBindingNoErrorsNotIgnoreUnknown() { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("age", 32); pvs.add("nonExisting", "someValue"); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
binder.setIgnoreUnknownFields(false); LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.afterPropertiesSet();
public static void main(String[] args) { Integer integer = new Integer(0); DataBinder dataBinder = new DataBinder(integer); dataBinder.setIgnoreUnknownFields(false); dataBinder.setIgnoreInvalidFields(false); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.addPropertyValue("value", 2); dataBinder.bind(propertyValues); System.out.println(integer); } }
/** * Create a binder for the target object. The binder will then be used to * bind the properties form a field set into the target object. This * implementation creates a new {@link DataBinder} and calls out to * {@link #initBinder(DataBinder)} and * {@link #registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)}. * * @param target the object to bind to. * @return a {@link DataBinder} that can be used to bind properties to the * target. */ protected DataBinder createBinder(Object target) { DataBinder binder = new DataBinder(target); binder.setIgnoreUnknownFields(!this.strict); initBinder(binder); registerCustomEditors(binder); return binder; }
/** * Create a binder for the target object. The binder will then be used to * bind the properties form a field set into the target object. This * implementation creates a new {@link DataBinder} and calls out to * {@link #initBinder(DataBinder)} and * {@link #registerCustomEditors(PropertyEditorRegistry)}. * * @param target Object to bind to * @return a {@link DataBinder} that can be used to bind properties to the * target. */ protected DataBinder createBinder(Object target) { DataBinder binder = new DataBinder(target); binder.setIgnoreUnknownFields(!this.strict); initBinder(binder); registerCustomEditors(binder); if(this.conversionService != null) { binder.setConversionService(this.conversionService); } return binder; }
private void doBind(Object bean, NacosConfigurationProperties properties, PropertyValues propertyValues) { DataBinder dataBinder = new DataBinder(bean); dataBinder.setAutoGrowNestedPaths(properties.ignoreNestedProperties()); dataBinder.setIgnoreInvalidFields(properties.ignoreInvalidFields()); dataBinder.setIgnoreUnknownFields(properties.ignoreUnknownFields()); dataBinder.bind(propertyValues); }
private void doBind(Object bean, NacosConfigurationProperties properties, PropertyValues propertyValues) { DataBinder dataBinder = new DataBinder(bean); dataBinder.setAutoGrowNestedPaths(properties.ignoreNestedProperties()); dataBinder.setIgnoreInvalidFields(properties.ignoreInvalidFields()); dataBinder.setIgnoreUnknownFields(properties.ignoreUnknownFields()); dataBinder.bind(propertyValues); }
@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@SuppressWarnings("unchecked") private void bindAndValidate(Map<String, String> raw) throws BindException { DataBinder dataBinder = new DataBinder(beanWrapper.getWrappedInstance()); dataBinder.setIgnoreUnknownFields(false); dataBinder.setConversionService(conversionService); MutablePropertySources mps = new MutablePropertySources(); mps.addFirst(new MapPropertySource("options", (Map) raw)); try { dataBinder.bind(new PropertySourcesPropertyValues(mps)); } catch (InvalidPropertyException e) { dataBinder.getBindingResult().addError(new FieldError("options", e.getPropertyName(), e.getMessage())); } CustomValidatorBean validator = new CustomValidatorBean(); validator.afterPropertiesSet(); dataBinder.setValidator(validator); Class<?>[] groups = determineGroupsToUse(beanWrapper.getWrappedInstance()); dataBinder.validate((Object[]) groups); if (dataBinder.getBindingResult().hasErrors()) { throw new BindException(dataBinder.getBindingResult()); } }