對象創(chuàng)建時機(jī)
<!--單例加載xml時創(chuàng)建,多例調(diào)用getbean時創(chuàng)建-->
<bean scope="singleton" id="hello" class="org.spring.HelloFactory" factory-method="getHello"></bean>
<bean scope="prototype" id="hello" class="org.spring.HelloFactory" factory-method="getHello"></bean>
單例生命周期
<bean init-method="init" destroy-method="des" scope="singleton" id="hello" class="org.spring.HelloFactory" factory-method="getHello"></bean>
容器銷毀觸發(fā)des方法
((ClassPathXmlApplicationContext) context).close();
多例生命周期
對象消亡利用java垃圾回收
di依賴注入
構(gòu)造函數(shù)注入
基本類型直接注入
對象類型先由容器創(chuàng)建對象后ref引用
<bean init-method="init" destroy-method="des" scope="singleton" id="hello" class="org.spring.Hello">
<constructor-arg name="age" value="24"></constructor-arg>
<constructor-arg name="date" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
必須重寫構(gòu)造方法
set方法注入
<bean init-method="init" destroy-method="des" scope="singleton" id="hello" class="org.spring.Hello">
<property name="age" value="25"></property>
</bean>
調(diào)用默認(rèn)構(gòu)造方法生成對象后調(diào)用set方法賦值
給復(fù)雜類型賦值
<bean init-method="init" destroy-method="des" scope="singleton" id="hello" class="org.spring.Hello">
<property name="arr">
<array>
<value>1</value>
<value>50</value>
</array>
</property>
</bean>