Spring之Bean配置

实体类代码

package cn.edu.zafu;


public class Car {
	private String brand;
	private String corp;
	private double price;
	private int maxSpeed;
	public Car(String brand, String corp, double price) {
		this.brand = brand;
		this.corp = corp;
		this.price = price;
	}
	public Car(String brand, String corp, int maxSpeed) {
		this.brand = brand;
		this.corp = corp;
		this.maxSpeed = maxSpeed;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
				+ ", maxSpeed=" + maxSpeed + "]";
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	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 int getMaxSpeed() {
		return maxSpeed;
	}
	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}
	
}



package cn.edu.zafu;


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 + "]";
	}
}


Spring属性注入通过反射创建Bean,要求必须提供一个默认的构造器,在代码中我们通过bean id获得bean


<bean id="helloWorld" class="cn.edu.zafu.HelloWorld">
	<property name="name" value="lizhangqu"></property>
</bean>



Spring构造器注入,可以使用index表示构造器中的第几个属性,使用type表示构造器参数类型


	<bean id="car" class="cn.edu.zafu.Car">
		<constructor-arg value="Audi" index="0"></constructor-arg>
		<constructor-arg value="Shanghai" index="1"></constructor-arg>
		<constructor-arg value="30000" index="2"></constructor-arg>
	</bean>


	<bean id="car1" class="cn.edu.zafu.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>


如过value值包含特殊字符,可以包含value标签,使用CDATA

<span style="white-space:pre">	</span><bean id="car2" class="cn.edu.zafu.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg type="java.lang.String">
			<value><![CDATA[<ShangHai>]]></value>
		</constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>

可以使用ref引用已有的bean

	<bean id="person" class="cn.edu.zafu.Person">
		<property name="name" value="Tom"></property>
		<property name="age" value="13"></property>
		<property name="car" ref="car2"></property>
	</bean>

也可以使用内部bean
<span style="white-space:pre">	</span><bean id="person1" class="cn.edu.zafu.Person">
		<property name="name" value="Tom"></property>
		<property name="age" value="13"></property>
		<property name="car">
			<bean class="cn.edu.zafu.Car">
				<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
				<constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
				<constructor-arg value="200" type="int"></constructor-arg>
			</bean>
		</property>
	</bean>

也可以使用级联属性,但是前提是先初始化

<span style="white-space:pre">	</span><bean id="person2" class="cn.edu.zafu.Person">
		<property name="name" value="Tom"></property>
		<property name="age" value="13"></property>
		<property name="car" ref="car2"></property>
		<property name="car.price" value="3000000"></property>
	</bean>

对于list,map等数据类型,可以只用对应的标签


你可能感兴趣的:(Spring之Bean配置)