Spring入门之二-------SpringIoC之实例化Bean以及注入Bean

时间:2024-09-02 13:06:08

一、实例化Bean

1. 通过默认构造方法实创建Bean

public class Bean1 {

    public Bean1() {
System.out.println(this.getClass().getSimpleName() + " has been created");
}
}

Bean1

<bean id="bean1" class="com.imooc.springClass2.inject.Bean1"/>

2. 通过静态工厂方法创建Bean

public class Bean1Factory {
public static Bean1 createBean1() {
return new Bean1();
}
}
<bean id="bean1" class="com.imooc.springClass2.inject.Bean1Factory" factory-method="createBean1"/>

3. 通过工厂实例方法创建Bean

public class Bean2Factory {
public Bean2 createBean2() {
return new Bean2();
}
}
<bean id="bean2Factory" class="com.imooc.springClass2.inject.Bean2Factory"/>
<bean id="bean2FromFactory" factory-bean="bean2Factory" factory-method="createBean2"/>

二、注入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 List<String> stringList;
private List<Bean2> bean2List;
private Map<String, Integer> simpleMap;
private Map<Bean1, Bean2> ObjectMap;
private Set<String> stringSet;
private Set<Bean2> 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

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<constructor-arg index="0" name="bean1" type="com.imooc.springClass2.inject.Bean1" ref="bean1"/>
<constructor-arg index="1" name="stringValue1" type="java.lang.String" value="aaaaa"/>
<constructor-arg index="2" name="integerValue1" type="java.lang.Integer" value="11111"/>
</bean>

其中,index、name、type无需全部都有,可区分是哪个参数即可。

简化版

<bean id="bean3FromSimple" class="com.imooc.springClass2.inject.Bean3" c:bean1-ref="bean1" c:stringValue1="aaaaa" c:integerValue1="11111"/>

2. 通过set方法注入Bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="bean2" ref="bean2"/>
<property name="stringValue2" value="bbbbb"/>
<property name="integerValue2" value="22222"/>
</bean>

简化版

<bean id="bean3FromSimple" class="com.imooc.springClass2.inject.Bean3" p:bean2-ref="bean2" p:stringValue2="bbbbb" p:integerValue2="22222"/>

3. 集合类型注入Bean

List

<bean id="bean2" class="com.imooc.springClass2.inject.Bean2"/>
<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="stringList">
<list>
<value>ccccc</value>
<value>ddddd</value>
</list>
</property>
<property name="bean2List">
<list>
<ref bean="bean2"/>
<ref bean="bean2"/>
</list>
</property>
</bean>

Map

<bean id="bean1" class="com.imooc.springClass2.inject.Bean1"/>
<bean id="bean2" class="com.imooc.springClass2.inject.Bean2"/>
<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="simpleMap">
<map>
<entry key="eeeee" value="33333"/>
<entry key="fffff" value="44444"/>
</map>
</property>
<property name="objectMap">
<map>
<entry key-ref="bean1" value-ref="bean2"/>
</map>
</property>
</bean>

Set

<bean id="bean2" class="com.imooc.springClass2.inject.Bean2"/>
<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="stringSet">
<set>
<value>eeeee</value>
<value>fffff</value>
</set>
</property>
<property name="bean2Set">
<set>
<ref bean="bean2"/>
</set>
</property>
</bean>

Properties

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
</props>
</property>
</bean>

4. null值注入Bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="stringValue3">
<null/>
</property>
</bean>

5. 注入内部bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="bean2">
<bean class="com.imooc.springClass2.inject.Bean2"/>
</property>
</bean>