The reason is that both beans actually have the same name foo
, so internally one bean definition is getting overridden with the other one, essentially the one with @Bean
is getting overridden by the one being scanned by @ComponentScan
.
The fix is simply to give one of them a different name and you should see the correct behavior of the @Primary
bean getting injected.
@Primary
@Bean
public Foo foo1()
{
return new Foo("Primary bean!!");
}
OR
@Component("foo1")
public class Foo
{
..