mybatis.mapperLocations= classpath*:mapper/*.xml
mybatis.defaultStatementTimeoutInSecond= 5
mybatis.mapUnderscoreToCamelCase= false
mybatis.unknownFields= exception
@ConfigurationProperties(prefix = "mybatis", ignoreInvalidFields = false, ignoreUnknownFields = false)
public class MyBatisProperties {
    private String basePackage;
    private String mapperLocations;
    private String typeAliasesPackage;
    private String markerInterface;
    private Integer defaultStatementTimeoutInSecond;
    private Boolean mapUnderscoreToCamelCase;
    private String configLocation;
}

Throw an exception

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@450794b4 type = com.example.demo.config.MyBatisProperties, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=false, ignoreUnknownFields=false, prefix=mybatis, value=mybatis)]] failed:

    Property: mybatis.unknownFields
    Value: false
    Origin: class path resource [application.properties]:7:13
    Reason: The elements [mybatis.aa] were left unbound.

but

@ConfigurationProperties(prefix = "mybatis", ignoreInvalidFields = true, ignoreUnknownFields = false)
public class MyBatisProperties {
  //.......
}

It works fine, but I think it's time to throw an exception

Comment From: wilkinsona

Thanks for the report. This looks like a bug to me.

The problem can be reproduced by modifying ConfigurationPropertiesTests to add ignoreInvalidFields = true to @ConfigurationProperties on IgnoreUnknownFieldsFalseProperties:

@ConfigurationProperties(ignoreUnknownFields = false, ignoreInvalidFields = true)
static class IgnoreUnknownFieldsFalseProperties extends BasicProperties {

}

With this change in place loadWhenHasIgnoreUnknownFieldsFalseAndUnknownFieldsShouldFail will fail. IgnoreErrorsBindHandler is catching and ignoring the UnboundConfigurationPropertiesException. I think we may be able to fix it by overriding onFailure in NoUnboundElementsBindHandler to prevent UnboundConfigurationPropertiesException from being ignored:

@Override
public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error)
        throws Exception {
    if (error instanceof UnboundConfigurationPropertiesException) {
        throw error;
    }
    return super.onFailure(name, target, context, error);
}

Flagging for team attention to double-check my reasoning and discuss which release we should fix this in.

Comment From: YangfanCheng1

I had ran into the same issue and I thought it was expected behavior. Open a PR using what @wilkinsona suggested.