How to set spring.main.allow-bean-definition-overriding to true in a Spring boot 2.1.0 starter configuration : <p>I maintain a spring-boot-starter that customizes the error attributes returned when, for instance, a unknown end point is called.
This is done by overriding the org.springframework.boot.web.servlet.error.ErrorAttributes bean.</p>

<p>Everything worked fine with 2.0.6, but <a href="https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes" rel="noreferrer">2.1.0 disables bean overriding by default</a>, making the starter now fail with the following message.</p>

<blockquote>
  <p>Invalid bean definition with name 'errorAttributes' defined in class
  path resource
  [com/mycompany/springboot/starter/config/ErrorsConfig.class]: Cannot
  register bean definition [Root bean: class [null]; scope=;
  abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0;
  autowireCandidate=true; primary=false;
  factoryBeanName=com.mycompany.springboot.starter.config.ErrorsConfig;
  factoryMethodName=errorAttributes; initMethodName=null;
  destroyMethodName=(inferred); defined in class path resource
  [com/mycompany/springboot/starter/config/ErrorsConfig.class]] for bean
  'errorAttributes': There is already [Root bean: class [null]; scope=;
  abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0;
  autowireCandidate=true; primary=false;
  factoryBeanName=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
  factoryMethodName=errorAttributes; initMethodName=null;
  destroyMethodName=(inferred); defined in class path resource
  [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.class]]
  bound</p>
</blockquote>

<p>As explained in documentation setting the spring.main.allow-bean-definition-overriding property to true fixes the problem.
My question is how to do that <strong>in the starter</strong> (I do not want the users of my starter to have to change their application.properties file, for something that is specific to my starter)?</p>

<p>I tried to a @PropertySource("classpath:/com/mycompany/starter/application.properties") annotation to my @Configuration with that property defined in that file, but it doesn't work.</p>

<p>What am I missing? Is there any way to allow my configuration overriding that bean?</p>

<p>Here is the (simplified) source code of the configuration:</p>

<pre><code>@Configuration
@PropertySource("classpath:/com/mycompany/starter/application.properties")
public class ErrorsConfig {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {
            @SuppressWarnings("unchecked")
            @Override
            public Map&lt;String, Object&gt; getErrorAttributes(WebRequest request, boolean includeStackTrace) {
                Map&lt;String, Object&gt; errorAttributes = super.getErrorAttributes(request, includeStackTrace);
                // CustomeError is a (simplified) bean of the error attributes we should return.
                CustomError err = new CustomError("myErrorCode", (String) errorAttributes.get("error"));
                return OBJECT_MAPPER.convertValue(err, Map.class);
            }
        };
    }
}
</code></pre>

<p>and my resource file com/mycompany/starter/application.properties contains</p>

<blockquote>
  <p>spring.main.allow-bean-definition-overriding=true</p>
</blockquote>
