字面值:
字面值:可用字符串表示的值,可以通过<value>元素标签或value属性进行注入。
<bean id="car" class="com.wq.spring.beans.Car">
<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg type="int">
<value>250</value>
</constructor-arg>
</bean>
基本数据类型及其封装类、String等类型都可以采取字面值注入的方式
若字面值中包含特殊字符,可以使用<![CDATA]]>把字面值包裹起来。
<constructor-arg type="java.lang.String">
<!-- 如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来 -->
<!-- 属性值也可以使用value直接点进行配置 -->
<value><![CDATA[<shanghai> ]]></value>
</constructor-arg>
引用其他Bean
- 组成应用程序的Bean经常需要相互作为完成应用程序的功能。要使Bean能够相互访问。就必须在Bean配置文件中指定对Bean的引用。
- 在Bean的配置文件中,可以通过<ref>元素或ref属性为Bean的属性或构造器参数指定对Bean的引用。
<!-- 通过构造方法来配置bean的属性 --> <bean id="car1" class="com.wq.spring.beans.Car"> <constructor-arg value="Audi" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <!-- 如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来 --> <!-- 属性值也可以使用value直接点进行配置 --> <value><![CDATA[<shanghai> ]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="com.wq.spring.beans.Person"> <property name="name" value="Tom"></property> <property name="age" value="23"></property> <!-- 可以使用property的ref属性建立bean之间的引用关系 --> <property name="car" ref="car1"></property> </bean>
<bean id="car1" class="com.wq.spring.beans.Car"> <constructor-arg value="Audi" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <!-- 如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来 --> <!-- 属性值也可以使用value直接点进行配置 --> <value><![CDATA[<shanghai> ]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="com.wq.spring.beans.Person"> <property name="name" value="Tom"></property> <property name="age" value="23"></property> <!-- 可以使用property的ref属性建立bean之间的引用关系 --> <property name="car"> <ref bean="car1"/> </property> </bean>
- 也可以在属性或构造器里包含Bean的声明。这样的Bean称为内部Bean。
<bean id="person" class="com.wq.spring.beans.Person"> <property name="name" value="Tom"></property> <property name="age" value="23"></property> <!-- 可以使用property的ref属性建立bean之间的引用关系 --> <property name="car"> <bean class="com.wq.spring.beans.Car"> <constructor-arg value="Ford"> </constructor-arg> <constructor-arg value="changan"></constructor-arg> <constructor-arg value="231"></constructor-arg> </bean> </property> </bean>
注入参数详解:null值和级联属性
- 可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型属性注入null值
ps:一般没有必要。
- 和Struts、Hibernate等框架一样。Spring支持级联属性的配置。
<bean id="car1" class="com.wq.spring.beans.Car">
<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
<constructor-arg type="java.lang.String">
<!-- 如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来 -->
<!-- 属性值也可以使用value直接点进行配置 -->
<value><![CDATA[<shanghai> ]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>250</value>
</constructor-arg>
</bean>
<bean id="person2" class="com.wq.spring.beans.Person">
<property name="name" value="Jarck"></property>
<property name="age" value="11"></property>
<property name="car" ref="car1"></property>
<!-- 为级联属性赋值。主要:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和
Struts2不同。-->
<property name="car.maxSpeed" value="260"></property>
</bean>
ps:一般不使用
集合属性
- 在Spring中可以通过一组内置的xml标签(例如:<list>,<set>或<map>)来配置集合属性。
public class Person {
private String name;
private int age;
private List<Car> cars;
......
}
public class Car {
private String brand;
private String corp;
private int price;
private int maxSpeed;
....
}
<beans>
<bean id="car2" class="com.wq.spring.beans.collection.Car">
<property name="brand" value="aode"></property>
<property name="corp" value="corp2"></property>
<property name="price" value="1200"></property>
</bean>
<bean id="car3" class="com.wq.spring.beans.collection.Car">
<property name="brand" value="aode3"></property>
<property name="corp" value="corp3"></property>
<property name="price" value="13000"></property>
</bean>
<bean id="person3" class="com.wq.spring.beans.collection.Person">
<property name="name" value="Mike"></property>
<property name="age" value="34"></property>
<!--使用list节点 对list属性赋值-->
<property name="cars">
<list>
<ref bean="car2"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
</beans>
- 配置java.util.List类型的属性,需要指定<list>标签,在标签里包含一些元素。这些标签可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用。通过<bean>指定内置Bean定义。通过<null/>指定空元素。甚至可以内嵌他的集合。
<bean id="person4" class="com.wq.spring.beans.collection.Person">
<property name="name" value="Mike"></property>
<property name="age" value="34"></property>
<property name="cars">
<list>
<ref bean="car2"/>
<ref bean="car2"/>
<bean id="car3" class="com.wq.spring.beans.collection.Car" >
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="changan"></constructor-arg>
<constructor-arg value="200000"></constructor-arg>
</bean>
</list>
</property>
</bean>
- 数组的定义和List一样,都使用<list>。
- 配置java.util.Set需要使用<set>标签,定义元素的方法与List一样。
- java.util.Map 通过<map>标签定义,<map>标签里可以使用多个<entry>作为子标签。每个条目包含一个键和一个值
<bean id="newPerson" class="com.wq.spring.beans.collection.NewPerson">
<property name="name" value="Rose"></property>
<property name="age" value="29"></property>
<property name="cars">
<!-- 使用<map>节点及map的entry子节点配置Map类型配置成员变量 -->
<map>
<entry key="aa" value-ref="car2"></entry>
<entry key="bb" value-ref="car3"></entry>
<entry key="cc" value-ref="car2"></entry>
</map>
</property>
</bean>
- 必须在<key>标签里定义键
- 因为键和值得类型没有限制,所以可以自由的为它们指定<value>,<ref>,<bean>或<null>元素。
- 可以将Map的键和值作为<entry>的属性定义:简单常量使用key和value来定义;Bean引用通过key-ref和value-ref属性定义
- 使用<props>定义java.util.Properties,该标签使用多个<prop>作为子标签。每个<prop>标签必须可以key属性。
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}
<bean id="dataSource" class="com.wq.spring.beans.collection.DataSource">
<property name="properties">
<!-- 使用props和prop子节点来为properties属性赋值-->
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
使用 utility scheme定义集合
- 使用基本的集合标签定义集合时,不能讲集合作为独立的Bean定义,导致其他Bean无法引用该集合,所以无法在不同Bean之间共享集合。
- 可以使用util schema里的集合标签定义独立的集合Bean。需要注意的是,必须在<beans>根元素里添加util schema定义
<!-- 配置单例的集合bean,以供多个bean进行引用 -->
<util:list id="cars">
<ref bean="car2"/>
<ref bean="car2"/>
</util:list>
<bean id="person5" class="com.wq.spring.beans.collection.Person">
<property name="name" value="jack"></property>
<property name="age" value="29"></property>
<property name="cars" ref="cars"></property>
</bean>
使用p命名空间
- 为了简化XML文件的配置,越来越多的xml文件采用属性而非子元素配置信息。
- Spring从2.5版本开始引入了一个新的p命名空间,可以通过<bean>元素属性的方式配置Bean的属性。
- 使用p命名空间后,基于XML的配置方式将进一步简化。
<!-- 通过p命名空间bean 的属性赋值,需要导入p命名空间,相当于传统的配置方式更加的简洁 -->
<bean id="person6" class="com.wq.spring.beans.collection.Person" p:age="40"
p:name="queen" p:cars-ref="cars"></bean>