假如有两个配置类
当初始化ApplicationContext时
两个配置类中的方法都会被运行。
配置类代码如下:
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService(){
System.out.println("配置类@bean给容器中添加组件");
return new HelloService();
}
}
@Configuration
public class MyAppConfig2 {
//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService2(){
System.out.println("配置类2@bean给容器中添加组件");
return new HelloService();
}
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot01HelloworldQuickApplicationTests {
@Autowired
ApplicationContext ioc;
/*两个配置文件类都会被运行,且初始化ApplicationContext时会运行两个@Bean对应的方法*/
@Test
public void iocTest(){
boolean b = ioc.containsBean("helloService2");
System.out.println(b);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容