一、实例化Bean
1. 通过默认构造方法实创建Bean
public class Bean1 { public Bean1() { System.out.println(this.getClass().getSimpleName() + " has been created"); }}
2. 通过静态工厂方法创建Bean
public class Bean1Factory { public static Bean1 createBean1() { return new Bean1(); }}
3. 通过工厂实例方法创建Bean
public class Bean2Factory { public Bean2 createBean2() { return new Bean2(); } }
二、注入Bean
public class Bean3 { private final Bean1 bean1; private final String stringValue1; private final Integer integerValue1; private Bean2 bean2; private String stringValue2; private Integer integerValue2; private ListstringList; private List bean2List; private Map simpleMap; private Map ObjectMap; private Set stringSet; private Set bean2Set; private Properties properties; private String stringValue3; public Bean3(Bean1 bean1, String stringValue1, Integer integerValue1) { } public Bean3(Bean1 bean1, String stringValue1, Integer integerValue1) { this.bean1 = bean1; this.stringValue1 = stringValue1; this.integerValue1 = integerValue1; System.out.println(this.getClass().getSimpleName() + " has been created"); } //get/set ......}
1. 通过构造方法注入Bean
其中,index、name、type无需全部都有,可区分是哪个参数即可。
简化版
2. 通过set方法注入Bean
简化版
3. 集合类型注入Bean
List
Map
Set
Properties
value1 value2
4. null值注入Bean
5. 注入内部bean