03springboot通过配置类给容器中添加组件

使用全注解方式
1.编写配置类
@Configuration //指明当前类是一个配置类,替代spring的*.xml配置文件
public class MyConfig{

	//将方法的返回值添加到容器中,容器中这个组件的默认id就是方法名
	@Bean
    public HelloService helloService(){
     		return new HelloService();
    }

}
测试容器中有无组件
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章