课程名称:Spring入门
课程章节:第4-2章 通过注解注入Bean
课程讲师: 西昆仑
课程内容:
1、注入Bean
1.1、通过构造方法注入Bean
代码示例:
//anotherBean.class
@Component
public class AnotherBean {
}
//myBean.class
@Component
public class myBean {
private AnotherBean anotherBean1;
@Autowired
public MyBean(AnotherBean anotherBean1){
this.anotherBean1 = anotherBean1;
}
……
}
运行结果:

1.2、通过set方法注入Bean
代码示例(差异部分):
//myBean.class
@Component
public class myBean {
private AnotherBean anotherBean2;
@Autowired
public setAnotherBean2(AnotherBean anotherBean2){
this.anotherBean2 = anotherBean2;
}
}

运行结果:

1.3、通过属性注入Bean
代码示例(差异部分):
//myBean.class
@Component
public class myBean {
@Autowired
private AnotherBean anotherBean3;
}


1.4、集合类Bean类型注入
List注入


Map注入


1.5、String、Integer类型直接赋值

1.6、SpringIoc容器内置接口实例注入

2、通过注解设定Bean的作用域
代码示例:

自定义作用域差异:


方法注入差异:


课程收获:越来越感觉Spring注解功能的强大,后续还要掌握更多的注解功能,继续加油。
随时随地看视频