07_配置文件中决定接口的实现类
【工程截图】
【Document.java】
package com.HigginCui.document; public interface Document { public void readDocument(); public void writeDocument(); }
【ExcelDocument.java】
package com.HigginCui.document; public class ExcelDocument implements Document{ @Override public void readDocument() { System.out.println("Excel read..."); } @Override public void writeDocument() { System.out.println("Excel write..."); } }
【PdfDocument.java】
package com.HigginCui.document; public class PdfDocument implements Document{ @Override public void readDocument() { System.out.println("Pdf read..."); } @Override public void writeDocument() { System.out.println("Pdf write..."); } }
【WordDocument.java】
package com.HigginCui.document; public class WordDocument implements Document{ @Override public void readDocument() { System.out.println("Word read..."); } @Override public void writeDocument() { System.out.println("Word write..."); } }
【DocumentManager.java】
package com.HigginCui.document; public class DocumentManager { private Document document; public DocumentManager(){} //可以利用构造方法进行赋值 public DocumentManager(Document document){ this.document=document; } //也可以利用setter方法进行赋值 public void setDocument(Document document) { this.document = document; } public Document getDocument() { return document; } public void read(){ this.document.readDocument(); } public void write(){ this.document.writeDocument(); } }
【applicationContext.xml】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="wordDocument" class="com.HigginCui.document.WordDocument"></bean> <bean id="excelDocument" class="com.HigginCui.document.ExcelDocument"></bean> <bean id="pdfDocument" class="com.HigginCui.document.PdfDocument"></bean> <!-- 把DocumentManager、wordDocument、excelDocument、pdfDocument都放入Spring容器 在Spring容器中的配置文件决定了documentManager中接口的实现类是什么 --> <bean id="documentManager" class="com.HigginCui.document.DocumentManager"> <!-- 该document接口是由下面的wordDocument类来实现 --> <property name="document"> <ref bean="wordDocument"/> </property> </bean> </beans>
【testDocument.java】
package com.HigginCui.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.HigginCui.document.DocumentManager; public class testDocument { @Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); DocumentManager documentManager=(DocumentManager) context.getBean("documentManager"); documentManager.read(); documentManager.write(); } }
【运行结果】
Word read...
Word write...
package com.HigginCui.document;
public interface Document {public void readDocument();public void writeDocument();}