Spring——第二章 Spring與IoC(二)
阿新 • • 發佈:2018-12-11
2.2.7 Bean的生命週期
Spring 內的Bean物件從建立到銷燬,可控點很多。
2.2.8 <bean>的id與name 屬性
name對格式沒有限制,id有
2.3 基於XML的DI
2.3.1 注入分類
設值注入:
基本屬性用 value
物件對應的屬性用 ref
<bean id="myStudent" class="com.bjpowernode.di01.Student"> <property name="name" value="張三"/> <property name="age" value="23"/> <property name="school" ref="mySchool"/> </bean>
構造注入:
少用
<bean id="myStudent" class="com.bjpowernode.di02.Student"> <constructor-arg name="name" value="李四"/> <constructor-arg name="age" value="24"/> <constructor-arg name="school" ref="mySchool"/> <!-- <constructor-arg index="0" value="李四"/> <constructor-arg index="1" value="24"/> <constructor-arg index="2" ref="mySchool"/> --> <!-- <constructor-arg value="李四"/> <constructor-arg value="24"/> <constructor-arg ref="mySchool"/> --> </bean>
2.3.3 集合屬性注入
properties 的鍵值對均為String型別,map自定義
①
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool" class="com.bjpowernode.di05.School"> <property name="name" value="清華大學"/> </bean> <bean id="mySchool2" class="com.bjpowernode.di05.School"> <property name="name" value="北京大學"/> </bean> <bean id="mySome" class="com.bjpowernode.di05.Some"> <property name="schools"> <array> <ref bean="mySchool"/> <ref bean="mySchool2"/> </array> </property> <property name="myStr"> <array> <value>中國</value> <value>北京</value> </array> </property> <property name="myList"> <list> <value>海淀</value> <value>六道口</value> </list> </property> <property name="mySet"> <set> <value>中國農業大學</value> <value>男生公寓</value> </set> </property> <property name="myMap"> <map> <entry key="mobile" value="1234567"/> <entry key="QQ" value="7654321"/> </map> </property> <property name="myProperties"> <props> <prop key="education">大學</prop> <prop key="gender">男</prop> </props> </property> </bean> </beans>
②:對於set,array,list等集合型別的屬性,資料注入時可採用簡寫的形式<property name="" value=", , ,">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mySchool" class="com.bjpowernode.di06.School">
<property name="name" value="清華大學"/>
</bean>
<bean id="mySchool2" class="com.bjpowernode.di06.School">
<property name="name" value="北京大學"/>
</bean>
<bean id="mySome" class="com.bjpowernode.di06.Some">
<property name="schools">
<array>
<ref bean="mySchool"/>
<ref bean="mySchool2"/>
</array>
</property>
<property name="myStr" value="中國,北京"/>
<property name="myList" value="海淀,六道口"/>
<property name="mySet" value="中國農業大學,男生公寓">
</property>
<property name="myMap">
<map>
<entry key="mobile" value="1234567"/>
<entry key="QQ" value="7654321"/>
</map>
</property>
<property name="myProperties">
<props>
<prop key="education">大學</prop>
<prop key="gender">男</prop>
</props>
</property>
</bean>
</beans>
2.3.4 對於域屬性的自動化注入
autowire="byName" 會從容器中查詢與實體類的域屬性同名的Bean的id,並將該Bean物件自動注入給該實體類的域屬性。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="school" class="com.bjpowernode.di07.School">
<property name="name" value="清華大學"/>
</bean>
<!-- 註冊Student
autowire="byName" 會從容器中查詢與實體類的域屬性同名的Bean的id,
並將該Bean物件自動注入給該實體類的域屬性。
-->
<bean id="myStudent" class="com.bjpowernode.di07.Student" autowire="byName">
<property name="name" value="張三"/>
<property name="age" value="23"/>
</bean>
</beans>
autowire="byType" 會從容器中將與實體類中域屬性型別具有is-a關係的bean找到,並將該bean自動注入到域屬性。
(因為可向上轉型)
同一個類的兩個物件和具有is-a關係的多個類的多個物件均不可。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mySchool" class="com.bjpowernode.di08.School">
<property name="name" value="清華大學"/>
</bean>
<!--
<bean id="mySchool2" class="com.bjpowernode.di08.PrimarySchool">
<property name="address" value="海淀區"/>
</bean>
-->
<!-- 註冊Student
autowire="byType" 會從容器中查詢與實體類的域屬性型別具有is-a關係的Bean,
並將該Bean物件自動注入給該實體類的域屬性。
-->
<bean id="myStudent" class="com.bjpowernode.di08.Student" autowire="byType">
<property name="name" value="張三"/>
<property name="age" value="23"/>
</bean>
</beans>
2.3.5 SPEL注入
可以使用靜態方法,
可以訪問其他Bean的屬性
直接計算表示式
呼叫其他Bean的方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myPerson" class="com.bjpowernode.di08.Person">
<property name="pname" value="李四"/>
<property name="page" value="#{T(java.lang.Math).random()*50}"/>
</bean>
<bean id="myStudent" class="com.bjpowernode.di08.Student">
<property name="name" value="#{myPerson.pname}"/>
<property name="age" value="#{myPerson.computeAge()}"/>
</bean>
</beans>