spring的注入引數(bean物件中又有bean屬性),beans.xml配置
阿新 • • 發佈:2019-01-28
<?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="people" class="com.java.people.People">
<property name="id" value="1"></property>
<property name="name" value="人1"></property>
<property name="age" value="11"></property>
<property name="dog"> //dog屬性又是一個bean
<bean id="dog2" class="com.java.dog.Dog">
<property name="name" value="jack"></property>
</bean>
</property>
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>111</value>
</list>
</property>
<property name="loves">
<set>
<value>唱歌</value>
<value>唱歌</value>
<value>跳舞</value>
</set>
</property>
<property name="works">
<map>
<entry>
<key><value>第一</value></key> //要注意key標籤裡還有value標籤
<value>上班</value>
</entry>
<entry>
<key><value>第二</value></key>
<value>下班</value>
</entry>
</map>
</property>
<property name="properties"> //properties和map相似 key-- value的形式
<props>
<prop key="上午">加班</prop>
<prop key="下午">還是加班</prop>
</props>
</property>
</bean>
<bean id="people2" class="com.java.people.People">
<property name="id" value="2"></property>
<property name="name" value="人2"></property>
<property name="age" value="22"></property>
<property name="dog">
<null></null>
</property>
<bean id="people" class="com.java.people.People">
<property name="id" value="1"></property>
<property name="name" value="人1"></property>
<constructor-arg type="String" value="tom"></constructor-arg>
</bean>
<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="people" class="com.java.people.People">
<property name="id" value="1"></property>
<property name="name" value="人1"></property>
<property name="age" value="11"></property>
<property name="dog"> //dog屬性又是一個bean
<bean id="dog2" class="com.java.dog.Dog">
<property name="name" value="jack"></property>
</bean>
</property>
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>111</value>
</list>
</property>
<property name="loves">
<set>
<value>唱歌</value>
<value>唱歌</value>
<value>跳舞</value>
</set>
</property>
<property name="works">
<map>
<entry>
<key><value>第一</value></key> //要注意key標籤裡還有value標籤
<value>上班</value>
</entry>
<entry>
<key><value>第二</value></key>
<value>下班</value>
</entry>
</map>
</property>
<property name="properties"> //properties和map相似 key-- value的形式
<props>
<prop key="上午">加班</prop>
<prop key="下午">還是加班</prop>
</props>
</property>
</bean>
<bean id="people2" class="com.java.people.People">
<property name="id" value="2"></property>
<property name="name" value="人2"></property>
<property name="age" value="22"></property>
<property name="dog">
<null></null>
</property>
</bean>
<property name="id" value="1"></property>
<property name="name" value="人1"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog"></property> //ref關聯dog物件
</bean>
<bean id="dog" class="com.java.dog.Dog"><constructor-arg type="String" value="tom"></constructor-arg>
</bean>
</beans>
* 有個<null></null>標籤 如果內部bean為空值的時候使用。
注入引數型別有那些:基本型別注入,beand的關聯注入ref="" , 內部bean的注入 ,null值,級聯屬性,集合型別屬性。