@Configuration
public class MyConfiguration {
@Bean
public Animal cat(){
return new Cat();
}
@Bean
public Animal dog(){
return new Dog();
}
}
注入用@Resource加上方法名,测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Resource(name="cat")
private Animal animal;
@Test
public void contextLoads() {
animal.say();
}
}