您好,欢迎来到好走旅游网。
搜索
您的当前位置:首页【SpringBoot】关于@Configuration

【SpringBoot】关于@Configuration

来源:好走旅游网

假如有两个配置类

当初始化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);
    }
}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- haog.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务