对于一个简单的 POJO:
@Component
public class Foo
{
private final String string;
public Foo()
{
this("Secondary ComponentScan??");
}
public Foo(String string)
{
this.string = string;
}
@Override
public String toString()
{
return string;
}
}
而这个配置
@Configuration
@ComponentScan(basePackageClasses = Foo.class)
public class TestConfiguration
{
@Primary
@Bean
public Foo foo()
{
return new Foo("Primary bean!!");
}
}
我期待以下测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfiguration.class)
public class Test
{
@Autowired
private Foo foo;
@Test
public void test()
{
System.out.println(foo);
}
}
打印出来,Primary Bean!!
但它返回Secondary ComponentScan??
而不是......
怎么会?@Primary的文档没有说它对组件扫描的 bean 失败了!