下面是bean配置中要用到的实体的类
package zhang.spring.enities;
public class Car {
private String brand;
private double price;
private String color;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Car(String brand, double price, String color) {
super();
this.brand = brand;
this.price = price;
this.color = color;
}
public Car() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", color=" + color + "]";
}
}
package zhang.spring.enities;
import java.util.List;
import java.util.Map;
public class Person {
private String name;
private int age;
private double weight;
private Car car;
private List<Son> sons;
private Map<String,Car> cars;
public Map<String, Car> getCars() {
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
public List<Son> getSons() {
return sons;
}
public void setSons(List<Son> sons) {
this.sons = sons;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.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 double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Person() {
}
public void show(){
System.out.println("你好,我叫:"+this.name);
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(String name, int age, double weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", weight=" + weight + ", car=" + car + ", sons=" + sons + "]";
}
package zhang.spring.enities;
public class Son {
private String sonName;
private int age;
private String sex;
public String getSonName() {
return sonName;
}
public void setSonName(String sonName) {
this.sonName = sonName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Son [sonName=" + sonName + ", age=" + age + ", sex=" + sex + "]";
}
public Son() {
// TODO Auto-generated constructor stub
}
}
2.applicationContext.xml文件,中来配置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"
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">
<!-- 配置person如果字段中包含>和<等和xml文件发生歧义的字符可以使用<![CDATA[]]>包裹起来 -->
<bean id="person1" class="zhang.spring.enities.Person">
<property name="name">
<value><![CDATA[zhangyukang<>]]></value>
</property>
<property name="age" value="20"></property>
<property name="weight" value="12.2"></property>
</bean>
<!-- 配置person2 -->
<bean id="person2" class="zhang.spring.enities.Person">
<constructor-arg value="wangweixin" type="java.lang.String"></constructor-arg>
<constructor-arg value="12" type="int"></constructor-arg>
<constructor-arg value="12.50" type="double"></constructor-arg>
</bean>
<bean id="person3" class="zhang.spring.enities.Person">
<constructor-arg value="wangweixin2" type="java.lang.String"></constructor-arg>
<constructor-arg value="15" type="int"></constructor-arg>
</bean>
<!-- 配置Car -->
<bean id="car1" class="zhang.spring.enities.Car">
<property name="brand" value="Aodi"></property>
<property name="price" value="250000"></property>
<property name="color" value="black"></property>
</bean>
<!-- 配置person4:给引用类型赋值操作 -->
<bean id="person4" class="zhang.spring.enities.Person">
<property name="name" value="zhangyukang"></property>
<property name="age" value="25"></property>
<!-- 注意级联属性的赋值的时候,前提是必须要有对象已经被创建,不同于struts2中的和hibernate. -->
<property name="car">
<bean class="zhang.spring.enities.Car">
<property name="brand" value="Baoma"></property>
<property name="price" value="1000000"></property>
</bean>
</property>
<!-- 支持级联属性的赋值操作 -->
<property name="car.color" value="yellow"></property>
</bean>
<!-- 配置Person5 -->
<bean id="person5" class="zhang.spring.enities.Person">
<property name="name" value="peter"></property>
<property name="car" ref="car1"></property>
<property name="age" value="18"></property>
<property name="weight" value="18"></property>
</bean>
<!-- 配置Person6个集合类型进行赋值操作 -->
<bean id="person6" class="zhang.spring.enities.Person">
<property name="name" value="zhangyukang"></property>
<property name="car" ref="car1"></property>
<property name="sons">
<list>
<ref bean="son1"/>
<ref bean="son2"/>
</list>
</property>
<property name="weight" value="12.3"></property>
</bean>
<!-- 配置person7集合类型可以使用ref进行引用 -->
<bean id="son1" class="zhang.spring.enities.Son">
<property name="age" value="15"></property>
<property name="sex" value="man"></property>
<property name="sonName" value="xiaoming"></property>
</bean>
<bean id="son2" class="zhang.spring.enities.Son">
<property name="age" value="15"></property>
<property name="sex" value="man"></property>
<property name="sonName" value="xiaoming"></property>
</bean>
<!-- 配置person8 Map类型属性的配置 -->
<bean id="person8" class="zhang.spring.enities.Person">
<property name="name" value="zhangyukang8"></property>
<property name="age" value="88"></property>
<property name="car" ref="car1"></property>
<property name="sons">
<list>
<ref bean="son1"/>
<ref bean="son2"/>
</list>
</property>
<property name="cars">
<map>
<entry key="北京" value-ref="car1"></entry>
<entry key="上海" value-ref="car1"></entry>
</map>
</property>
</bean>
<!-- 配置properties类型的属性 -->
<bean id="data" class="zhang.spring.enities.DataSource">
<property name="data">
<props>
<prop key="user">zhangyukang</prop>
<prop key="password">305316</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
<prop key="jdbcurl">jdbc:mysql///test</prop>
</props>
</property>
</bean>
<!-- 配置外部的集合,已供其他的bean引用 注意:需要导入util 的namespace -->
<util:list id="sons">
<ref bean="son1"/>
<ref bean="son2"/>
</util:list>
<util:map id="cars">
<entry key="beijing" value-ref="car1"></entry>
<entry key="jiangxi" value-ref="car1"></entry>
</util:map>
<bean id="person9" class="zhang.spring.enities.Person">
<property name="name" value="zhangyukang9"></property>
<property name="age" value="55"></property>
<property name="sons" ref="sons"></property>
<property name="cars" ref="cars"></property>
</bean>
<!-- 使用p命名空间 -->
<bean id="P-car" class="zhang.spring.enities.Car" p:brand="LUHU"
p:color="blue" p:price="1212121" ></bean>
</beans>
测试类:
**package zhang.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zhang.spring.enities.DataSource;
import zhang.spring.enities.Person;
import zhang.spring.helloword.Car;
public class TestSpringBeans {
private ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“applicationContext.xml”);
/**
* 在applicationContext中配置bean,给bean的属性赋值
*/
@Test
public void testpersons(){
Person bean = (Person) applicationContext.getBean("person1");
bean.show();
}
/**
* 通过构造器给bean赋值
*1.可以通过设置construct节点的type熟悉,通过构造器的方式给bean赋值
*2.value:给属性赋上相应的值,对于string类型spring可以自动的进行类型转化.
*/
@Test
public void testperson2(){
Person person=(Person) applicationContext.getBean("person2");
person.show();
person=(Person) applicationContext.getBean("person3");
person.show();
}
//给应用类型进行赋值
@Test
public void testCarandperson(){
Person bean = (Person) applicationContext.getBean("person4");
System.out.println(bean);
Person person=(Person) applicationContext.getBean("person5");
System.out.println(person);
}
//给集合属性进行赋值操作
@Test
public void testListBean(){
Person bean = (Person) applicationContext.getBean("person6");
System.out.println(bean);
}
//Map类型的属性进行赋值
@Test
public void testMapBean(){
Person person=(Person) applicationContext.getBean("person8");
System.out.println(person.getCars());
}
//给properties属性进行赋值操作
@Test
public void testProperties(){
DataSource bean = (DataSource) applicationContext.getBean("data");
System.out.println(bean);
}
//引用外部的集合list和map
@Test
public void testbeanwithMapandList(){
zhang.spring.enities.Car bean = (zhang.spring.enities.Car) applicationContext.getBean("P-car");
System.out.println(bean);
}
//使用p命名空间个属性进行赋值操作
@Test
public void testPcar(){
Car car=(Car) applicationContext.getBean("P-car");
System.out.println(car);
}
}**