在<constructor-arg value="240" type="int"></constructor-arg>中 ,"240"被自动转为int的240。
可用字符串表示的值,可以通过<value>元素标签或value属性进行注入。
<constructor-arg type="int"> <value>240</value> </constructor-arg>基本数据类型及其封装类,String等类型都可以采取字面值注入的方式
若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来
<constructor-arg type="java.lang.String"> <value><![CDATA[<shanghai>]]></value> </constructor-arg>
<bean id="person" class="com.wul.spring.beans.Person"> <property name="name" value="wul"></property> <property name="age" value="22"></property> <!-- 引用其他的Bean --> <property name="car" ref="car2"></property> </bean>
<bean id="person2" class="com.wul.spring.beans.Person"> <property name="name" value="wallen"></property> <property name="age" value="33"></property> <property name="car"> <!--内部bean,不能被外部引用,只能在内部使用, 不必写id --> <bean class="com.wul.spring.beans.Car"> <property name="brand" value="benci"></property> <property name="corp" value="hefei"></property> <property name="price" value="350000"></property> <property name="maxSpeed" value="300"></property> </bean> </property> </bean>
<property name="car"><null/></property>
<!-- 为级联属性赋值,但注意car2的price值也会变为450000 --> <bean id="person3" class="com.wul.spring.beans.Person"> <property name="name" value="wul"></property> <property name="age" value="22"></property> <property name="car" ref="car2"></property> <property name="car.price" value="450000"></property> </bean>
<!-- 测试如何配置List属性 --> <bean id="person4" class="com.wul.spring.beans.collection.PersonList"> <property name="name" value="william"></property> <property name="age" value="25"></property> <property name="cars"> <list> <ref bean="car2"/> <ref bean="car"/> </list> </property> </bean>
<!-- 测试如何配置Map属性 --> <bean id="person5" class="com.wul.spring.beans.collection.PersonMap"> <property name="name" value="williammap"></property> <property name="age" value="256"></property> <property name="cars"> <!-- 使用map节点及map的entry子节点配置Map类型的成员变量 --> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean>
<!-- 配置Properties属性值 --> <bean id="dataSource" class="com.wul.spring.beans.collection.DataSource"> <property name="properties"> <!-- 使用props和prop子节点来为Properties属性赋值 --> <props> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean>
<!-- 配置独立的集合bean,以供多个bean共享引用,需要导入util命名空间 --> <util:list id="carsList"> <ref bean="car"/> <ref bean="car2"/> </util:list> <util:map id="carMap"> <entry key="aa" value-ref="car"/> <entry key="bb" value-ref="car2"/> </util:map> <util:properties id="propertiesutil"> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </util:properties> <bean id="person6" class="com.wul.spring.beans.collection.PersonList"> <property name="name" value="will"></property> <property name="age" value="21"></property> <property name="cars" ref="carsList"></property> </bean> <bean id="dataSourceutil" class="com.wul.spring.beans.collection.DataSource"> <property name="properties" ref="propertiesutil"></property> </bean>
<!-- 通过p命名空间为bean读属性赋值,需先导入p命名空间 --> <bean id="person7" class="com.wul.spring.beans.collection.PersonList" p:age="30" p:name="wulp" p:cars-ref="carsList"> </bean>

package com.wul.spring.beans;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public Car(){
}
public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", maxSpeed=" + maxSpeed + "]";
}
}
package com.wul.spring.beans;
public class Person {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
package com.wul.spring.beans.collection;
import java.util.List;
import com.wul.spring.beans.Car;
public class PersonList {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + cars + "]";
}
}
package com.wul.spring.beans.collection;
import java.util.List;
import java.util.Map;
import com.wul.spring.beans.Car;
public class PersonMap {
private String name;
private int age;
private Map<String,Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Car> getCars() {
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + cars + "]";
}
}
package com.wul.spring.beans.collection;
import java.util.Properties;
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 + "]";
}
}
<xml version="1.0" encoding="UTF-8"> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="car" class="com.wul.spring.beans.Car"> <constructor-arg value="Audi" index="0"></constructor-arg> <constructor-arg value="shanghai" index="1"></constructor-arg> <constructor-arg value="300000" type="double"></constructor-arg> </bean> <bean id="car2" class="com.wul.spring.beans.Car"> <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg> <!-- <constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>--> <!--如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来--> <constructor-arg type="java.lang.String"> <value><![CDATA[<shanghai>]]></value> </constructor-arg> <!-- 属性值也可以使用value子节点进行配置 --> <!-- <constructor-arg value="240" type="int"></constructor-arg> --> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="com.wul.spring.beans.Person"> <property name="name" value="wul"></property> <property name="age" value="22"></property> <!-- 引用其他的Bean --> <property name="car" ref="car2"></property> </bean> <bean id="person2" class="com.wul.spring.beans.Person"> <property name="name" value="wallen"></property> <property name="age" value="33"></property> <property name="car"> <!--内部bean,不能被外部引用,只能在内部使用, 不必写id --> <bean class="com.wul.spring.beans.Car"> <property name="brand" value="benci"></property> <property name="corp" value="hefei"></property> <property name="price" value="350000"></property> <property name="maxSpeed" value="300"></property> </bean> </property> <!-- 为级联属性赋值,注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 --> <property name="car.price" value="350000"></property> </bean> <!-- 为级联属性赋值,但注意car2的price值也会变为450000 --> <bean id="person3" class="com.wul.spring.beans.Person"> <property name="name" value="wul"></property> <property name="age" value="22"></property> <property name="car" ref="car2"></property> <property name="car.price" value="450000"></property> </bean> <!-- 测试如何配置List属性 --> <bean id="person4" class="com.wul.spring.beans.collection.PersonList"> <property name="name" value="william"></property> <property name="age" value="25"></property> <property name="cars"> <list> <ref bean="car2"/> <ref bean="car"/> </list> </property> </bean> <!-- 测试如何配置Map属性 --> <bean id="person5" class="com.wul.spring.beans.collection.PersonMap"> <property name="name" value="williammap"></property> <property name="age" value="256"></property> <property name="cars"> <!-- 使用map节点及map的entry子节点配置Map类型的成员变量 --> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean> <!-- 配置Properties属性值 --> <bean id="dataSource" class="com.wul.spring.beans.collection.DataSource"> <property name="properties"> <!-- 使用props和prop子节点来为Properties属性赋值 --> <props> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <!-- 配置独立的集合bean,以供多个bean共享引用,需要导入util命名空间 --> <util:list id="carsList"> <ref bean="car"/> <ref bean="car2"/> </util:list> <util:map id="carMap"> <entry key="aa" value-ref="car"/> <entry key="bb" value-ref="car2"/> </util:map> <util:properties id="propertiesutil"> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </util:properties> <bean id="person6" class="com.wul.spring.beans.collection.PersonList"> <property name="name" value="will"></property> <property name="age" value="21"></property> <property name="cars" ref="carsList"></property> </bean> <bean id="dataSourceutil" class="com.wul.spring.beans.collection.DataSource"> <property name="properties" ref="propertiesutil"></property> </bean> <!-- 通过p命名空间为bean读属性赋值,需先导入p命名空间 --> <bean id="person7" class="com.wul.spring.beans.collection.PersonList" p:age="30" p:name="wulp" p:cars-ref="carsList"> </bean> </beans>
package com.wul.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wul.spring.beans.collection.DataSource;
import com.wul.spring.beans.collection.PersonList;
import com.wul.spring.beans.collection.PersonMap;
public class Main {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
Person person2 = (Person) ctx.getBean("person2");
System.out.println(person2);
Person person3 = (Person) ctx.getBean("person3");
System.out.println(person3);
PersonList person4 = (PersonList) ctx.getBean("person4");
System.out.println(person4);
PersonMap person5 = (PersonMap) ctx.getBean("person5");
System.out.println(person5);
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
PersonList person6 = (PersonList) ctx.getBean("person6");
System.out.println(person6);
DataSource dataSource2 = (DataSource) ctx.getBean("dataSourceutil");
System.out.println(dataSource2);
PersonList person7 = (PersonList) ctx.getBean("person7");
System.out.println(person7);
}
}
热门源码