1.创建项目,项目名称(springdemo2),如图所示
2.在项目中创建相应的目录(lib->jar包目录,src->源码目录,test->测试目录),目录结构如图所示
3.在lib目录中创建子目录(apache,junit,spring)存储相应的jar包,如图所示
4.分别在apache,junit,spring中添加对应的jar包,如图所示
5.对jar包进行build path,build path的过程如果不会请参照(Spring4-HelloWorld)这篇文章,jar build后目录
如图所示
6.在src源码目录下创建接口IOutputGenerator,包名(main.java.com.mycompany.service),如图所示
7.接口IOutputGenerator的内容如下
package main.java.com.mycompany.service; public interface IOutputGenerator { public void generateOutput(); }
8.在src源码目录中创建接口IOutputGenerator的两个实现类CsvOutputGenerator,JsonOutputGenerator,包名(main.java.com.mycompany.service.impl),如图所示
9.实现类CsvOutputGenerator的内容如下
package main.java.com.mycompany.service.impl; import main.java.com.mycompany.service.IOutputGenerator; public class CsvOutputGenerator implements IOutputGenerator { @Override public void generateOutput() { System.out.println("Csv Output Generator"); } }
10.实现类JsonOutputGenerator的内容如下
package main.java.com.mycompany.service.impl; import main.java.com.mycompany.service.IOutputGenerator; public class JsonOutputGenerator implements IOutputGenerator{ @Override public void generateOutput() { System.out.println("Json Output Generator"); } }
11.在src源码目录创建操作类OutputHelper,包名(main.java.com.mycompany.util),如图所示
12.操作类OutputHelper的内容如下
package main.java.com.mycompany.util; import main.java.com.mycompany.service.IOutputGenerator; public class OutputHelper { IOutputGenerator outputGenerator; public void generateOutput(){ outputGenerator.generateOutput(); } public void setOutputGenerator(IOutputGenerator outputGenerator){ this.outputGenerator = outputGenerator; } }
13.在src源码目录创建配置文件applicationContext.xml,如图所示
14.配置文件applicationContext.xml的内容如下
<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.0.xsd"> <bean id="OutputHelper" class="main.java.com.mycompany.util.OutputHelper"> <property name="outputGenerator" ref="CsvOutputGenerator"></property> </bean> <bean id="CsvOutputGenerator" class="main.java.com.mycompany.service.impl.CsvOutputGenerator"> </bean> <bean id="JsonOutputGenerator" class="main.java.com.mycompany.service.impl.JsonOutputGenerator"> </bean> </beans>
15.在test测试目录中创建测试类OutputHelperTest,包名(main.java.com.mycompany.util),如图所示
16.测试类OutputHelperTest的内容如下
package main.java.com.mycompany.util; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class OutputHelperTest { @Test public void outputHelperTest(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); OutputHelper output = (OutputHelper) context.getBean("OutputHelper"); output.generateOutput(); } }
17.在outputHelperTest方法上右键运行Run as -> Junit Test,运行结果如图所示
18.修改applicationContext.xml文件,如图所示
19.修改applicationContext.xml文件的内容如下
<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.0.xsd"> <bean id="OutputHelper" class="main.java.com.mycompany.util.OutputHelper"> <property name="outputGenerator" ref="JsonOutputGenerator"></property> </bean> <bean id="CsvOutputGenerator" class="main.java.com.mycompany.service.impl.CsvOutputGenerator"> </bean> <bean id="JsonOutputGenerator" class="main.java.com.mycompany.service.impl.JsonOutputGenerator"> </bean> </beans>
20.再运行OutputHelperTest类中的outputHelperTest方法,如图所示
21.在没有对代码进行任何的修改情况下,只修改了applicationContext.xml文件,则切换了接口的不同实现方式.
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1908705