通常,当你为一个Bean指定了class属性,就将要求Spring IoC容器调用构造程序创建Bean。
如下,有一个Product类,他有两个子类Battery和Disc。
Product类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.springrecipesnote.springioc.shop;
/**
* Title: Product.java
* 产品抽象类
*
* @author jackie
* @since Apr 14, 2013 8:09:49 PM
* @version V1.0
*/
public abstract class Product {
private String name;
private double price;
public Product() {
}
/**
* <p>Title: </p>
* <p>含参构造函数</p>
* @param name
* @param price
*/
public Product(String name, double price) {
this.name = name;
this.price = price;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return name + " " + price;
}
}
Battery类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.springrecipesnote.springioc.shop;
/**
* Title: Battery.java 类的功能说明
*
* @author jackie
* @since Apr 14, 2013 8:14:26 PM
* @version V1.0
*/
public class Battery extends Product {
private boolean rechargeable;
public Battery() {
super();
}
public Battery(String name, double price) {
super(name, price);
}
/**
* @return the rechargeable
*/
public boolean isRechargeable() {
return rechargeable;
}
/**
* @param rechargeable
* the rechargeable to set
*/
public void setRechargeable(boolean rechargeable) {
this.rechargeable = rechargeable;
}
}
Disc类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.springrecipesnote.springioc.shop;
/**
* Title: Disc.java 类的功能说明
*
* @author jackie
* @since Apr 14, 2013 8:18:30 PM
* @version V1.0
*/
public class Disc extends Product {
private int capacity;
public Disc() {
super();
}
public Disc(String name, double price) {
super(name, price);
}
/**
* @return the capacity
*/
public int getCapacity() {
return capacity;
}
/**
* @param capacity
* the capacity to set
*/
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="aaa" class="com.jackie.codeproject.springrecipesnote.springioc.shop.Battery">
<property name="name" value="AAA" />
<property name="price" value="2.5" />
<property name="rechargeable" value="true" />
</bean>
<bean id="cdrw" class="com.jackie.codeproject.springrecipesnote.springioc.shop.Disc">
<property name="name" value="CD-RW" />
<property name="price" value="1.5" />
<property name="capacity" value="700" />
</bean>
</beans>
如果没有指定<constructor-arg>元素,将会调用默认的不带参数的构造程序。然后对于每个<property>元素,Spring将通过setter方法注入值。前面的Bean配置相当于:
Product aaa = new Battery();
aaa.setName("AAA");
aaa.setPrice(2.5);
aaa.setRechargeable(true);
Product aaa = new Battery();
aaa.setName("AAA");
aaa.setPrice(2.5);
aaa.setCapacity(700);
相反,如果有一个或者多个<constructor-arg>元素,Spring将调用匹配参数最合适的构造程序。
以下是测试函数:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.springrecipesnote.springioc.shop;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Title: ProductTest.java 类的功能说 测试类
*
* @author jackie
* @since Apr 14, 2013 8:31:45 PM
* @version V1.0
*/
public class ProductTest {
private ApplicationContext applicationContext;
@Test
public void testProduct() {
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Product aaa = (Product) applicationContext.getBean("aaa");
Product cdrw = (Product) applicationContext.getBean("cdrw");
System.out.println(aaa);
System.out.println(cdrw);
}
@After
public void after() {
if (null != applicationContext) {
applicationContext = null;
}
}
}