配置Bean
Ioc容器
Ioc容器需要实例化以后才可以从Ioc容器里获取bean实例并使用。
spring提供两种方式类型的Ioc容器实现:
BeanFactory:底层的,面向spring框架的。
ApplicationContext :面向开发人员的,一般用这个。
有两个实现类:
- ClassPathXmlApplicationContext:从类路径下加载配置文件。
- FileSystemXmlApplicationContext:从文件系统中加载配置文件。
两种方式配置文件是相同的。
通过xml方式配置bean
示例:
<!--xml的方式配置bean -->
<bean id="productBean" class="com.pfSoft.beans.ProductEntity">
<property name="prodNo" value="牙膏"></property>
<property name="prodName" value="筷子"></property>
</bean>
class: 由示例中,可以看到class使用了全类名,因此spring是通过反射的方式在Ioc容器中创建bean的,因此bean中必须有无参的构造函数。
ProductEntity productEntity= (ProductEntity) applicationContext.getBean("productBean");
id:在获取bean的时候getBean中的参数,是id的名字。即配置bean时候取的名称。(标示容器中的 bean id,唯一)
获取Bean的方式:
getBean方法有几个重载,通过查看BeanFactory接口定义可以发现,有如下重载:

ProductEntity productEntity1= (ProductEntity) applicationContext.getBean("productBean"); //使用beanId获取
System.out.println(productEntity1.getProdNo());
System.out.println(productEntity1.getProdName());
ProductEntity productEntity2= applicationContext.getBean(ProductEntity.class); //使用类型获,但是该类型必须在Ioc中容器唯一,否则会报错
System.out.println(productEntity2.getProdNo());
System.out.println(productEntity2.getProdName());
public class Order {
public Order(String orderNo,BigDecimal price,String customer){
this.orderNo=orderNo;
this.price=price;
this.customerName=customer;
}
private String orderNo;
private BigDecimal price;
private String customerName;
/**
*
* @return the orderNo
*/
public String getOrderNo() {
return orderNo;
}
/**
* @param orderNo the orderNo to set
*/
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
/**
*
* @return the price
*/
public BigDecimal getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(BigDecimal price) {
this.price = price;
}
/**
*
* @return the customerName
*/
public String getCustomerName() {
return customerName;
}
/**
* @param customerName the customerName to set
*/
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Order [orderNo=" + orderNo + ", price=" + price
+ ", customerName=" + customerName + "]";
}
}
<!--用构造函数的方式注入
value:代表给参数赋的值。
index:构造函数中第几个方法,从0开始。
type:方法的类型。
当有多个构造函数重载时,需要通过index和type结合使用来确定用哪个构造函数实例化
-->
<bean id="OrderBean" class="com.pfSoft.beans.Order">
<constructor-arg value="1001" index="0" ></constructor-arg>
<constructor-arg value="33.333" index="1" ></constructor-arg>
<constructor-arg value="pf" index="2" ></constructor-arg>
</bean>
@Test
public void testConstructDep() {
Order order=(Order) applicationContext.getBean("OrderBean");
System.out.println(order);
}
<bean id="OrderBean" class="com.pfSoft.beans.Order">
<constructor-arg value="1001" index="0" ></constructor-arg>
<constructor-arg index="1" >
<value>33.333</value> <!--value属性, 也支持用value子节点的形式配置 -->
</constructor-arg>
<constructor-arg index="2">
<value><![CDATA[<PF!@#$>]]></value> <!-- 如果包含特殊字符可以使用<![CDATA[]]> 包裹起来 -->
</constructor-arg>
</bean>