SpringBoot – BeanDefinitionOverrideException: Invalid bean definition

Bean overriding has to be enabled since Spring Boot 2.1, https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes Bean Overriding Bean overriding has been disabled by default to prevent a bean being accidentally overridden. If you are relying on overriding, you will need to set spring.main.allow-bean-definition-overriding to true. Set spring.main.allow-bean-definition-overriding=true or yml, spring: main: allow-bean-definition-overriding: true to enable overriding again. Edit, Bean … Read more

Spring choose bean implementation at runtime

1. Implement a custom Condition public class LinuxCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty(“os.name”).contains(“Linux”); } } Same for Windows. 2. Use @Conditional in your Configuration class @Configuration public class MyConfiguration { @Bean @Conditional(LinuxCondition.class) public MyService getMyLinuxService() { return new LinuxService(); } @Bean @Conditional(WindowsCondition.class) public MyService getMyWindowsService() { return … Read more

Is there a way to @Autowire a bean that requires constructor arguments?

You need the @Value annotation. A common use case is to assign default field values using “#{systemProperties.myProp}” style expressions. public class SimpleMovieLister { private MovieFinder movieFinder; private String defaultLocale; @Autowired public void configure(MovieFinder movieFinder, @Value(“#{ systemProperties[‘user.region’] }”) String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } // … } See: Expression Language > Annotation … Read more