

- 通过set方式赋值
- 基本类型都可以用value来赋值(包装类型和string类型)

- 引用类型用ref赋值


- list类型的赋值,set的赋值与list一样

- map类型的赋值

- properties类型的赋值




Document类
public interface Document {
public void read();
public void write();
}
DocumentManager类
/**
* 利用spring的ioc和di做到了完全的面向接口编程
*@author Administrator
*
*/
public class DocumentManager {
private Document document;
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public void read(){
this.document.read();
}
public void write(){
this.document.write();
}
}
ExcelDocument、PDFDocument、WordDocument类
public class ExcelDocument implements Document{
@Override
public void read() {
// TODO Auto-generated method stub
System.out.println("excel read");
}
@Override
public void write() {
// TODO Auto-generated method stub
System.out.println("excel writer");
}
}
DocumentTest类
import org.junit.Test;
import cn.itcast.spring0909.utils.SpringHelper;
public class DocumentTest extends SpringHelper{
static{
path = "cn/itcast/spring0909/document/spring/applicationContext.xml";
}
@Test
public void test(){
DocumentManager documentManager = (DocumentManager)context.getBean("documentManager");
documentManager.read();
}
}
<?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-2.5.xsd">
<!-- 把documentManager,worddocument,exceldocument,pdfdocument -->
<bean id="documentManager" class="cn.itcast.spring0909.document.spring.DocumentManager">
<!--
document是一个接口
-->
<property name="document">
<!--
通过ref给接口赋值
-->
<ref bean="wordDocument"/>
</property>
</bean>
<bean id="wordDocument" class="cn.itcast.spring0909.document.spring.WordDocument"></bean>
<bean id="excelDocument" class="cn.itcast.spring0909.document.spring.ExcelDocument"></bean>
<bean id="pdfDocument" class="cn.itcast.spring0909.document.spring.PDFDocument"></bean>
</beans>
