/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.config.spring.context.properties;

import org.apache.dubbo.config.AbstractConfig;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.DataBinder;

import java.util.Map;

import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;

/**
 * Default {@link DubboConfigBinder} implementation based on Spring {@link DataBinder}
 */
public class DefaultDubboConfigBinder extends AbstractDubboConfigBinder {

    @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);
    }

}


@@ -1001,41 +1001,202 @@
 ion.
-DataBinder;%0A%0Aimport java.util.Map
+BindingResult;%0Aimport org.springframework.validation.DataBinder;%0Aimport org.springframework.validation.FieldError;%0A%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.stream.IntStream
 ;%0A%0Ai
@@ -2111,14 +2111,1011 @@
 s);%0A    
+    BindingResult bindingResult = dataBinder.getBindingResult();%0A        if (bindingResult.hasGlobalErrors()) %7B%0A            throw new RuntimeException(%22Data bind global error, please check config. config: %22 + bindingResult.getGlobalError() + %22%22);%0A        %7D%0A        if (bindingResult.hasFieldErrors()) %7B%0A            throw new RuntimeException(buildErrorMsg(bindingResult.getFieldErrors(), prefix, dubboConfig.getClass().getSimpleName()));%0A        %7D%0A    %7D%0A%0A    private String buildErrorMsg(List%3CFieldError%3E errors, String prefix, String config) %7B%0A        StringBuilder builder = new StringBuilder(%22Data bind error, please check config. config: %22 + config + %22, prefix: %22 + prefix%0A                + %22 , error fields: %5B%22 + errors.get(0).getField());%0A        if (errors.size() %3E 1) %7B%0A            IntStream.range(1, errors.size()).forEach(i -%3E %7B%0A                builder.append(%22, %22 + errors.get(i).getField());%0A            %7D);%0A        %7D%0A        builder.append(%22%5D%22);%0A        return builder.toString();%0A    
 %7D
-%0A
 %0A%7D%0A%0A

throw exception when data bind from properties file encounter error (#4757)