转自 https://blog.csdn.net/coding_1994/article/details/80634810,这位做者写的很清晰。java
//定义Person接口 public interface Person{ //Person接口里定义一个使用斧子的方法 public void useAxe(); }
//定义Axe接口 public interface Axe{ //Axe接口里有个砍的方法 public void chop(); }
public class Chinese implements Person{ //面向Axe接口编程,而不是具体的实现类 private Axe axe; //默认的构造器 public Chinese(){ } //设值注入所需的setter方法 public void setAxe(Axe axe){ this.axe = axe; } //实现Person接口的useAxe方法 public void useAxe(){ System.out.println(axe.chop()); } }
//Axe的第一个实现类 StoneAxe public class StoneAxe implements Axe{ //默认构造器 public StoneAxe(){ } //实现Axe接口的chop方法 public String chop(){ return "石斧砍柴好慢"; } }
<!-- 下面是标准的XML文件头 --> <?xml version="1.0" encoding="gb2312"?> <!-- 下面一行定义Spring的XML配置文件的dtd --> "http://www.springframework.org/dtd/spring-beans.dtd"> <!-- 以上三行对全部的Spring配置文件都是相同的 --> <!-- Spring配置文件的根元素 --> <BEANS> <!—定义第一bean,该bean的id是chinese, class指定该bean实例的实现类 --> <BEAN class="lee".Chinese id=chinese> <!-- property元素用来指定须要容器注入的属性,axe属性须要容器注入此处是设值注入,所以Chinese类必须拥有setAxe方法 --> |
<property name="axe"> <!-- 此处将另外一个bean的引用注入给chinese bean --> <REF local="”stoneAxe”/"> </property> </BEAN> <!-- 定义stoneAxe bean --> <BEAN class="lee".StoneAxe id=stoneAxe /> </BEANS> |
public class BeanTest{ //主方法,程序的入口 public static void main(String[] args)throws Exception{ //由于是独立的应用程序,显式地实例化Spring的上下文。 ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //经过Person bean的id来获取bean实例,面向接口编程,所以 //此处强制类型转换为接口类型 Person p = (Person)ctx.getBean("chinese"); //直接执行Person的userAxe()方法。 p.useAxe(); } }
<?xml version="1.0"?> <!-- 定义编译该项目的基本信息--> <PROJECT name="spring" default="." basedir="."> <!-- 定义编译和运行该项目时所需的库文件 --> <PATH id=classpath> <!-- 该路径下存放spring.jar和其余第三方类库 --> <FILESET dir=../../lib> <INCLUDE name="*.jar" /> </FILESET> <!-- 同时还须要引用已经编译过的class文件--> <PATHELEMENT path="." /> </PATH> <!-- 编译所有的java文件--> <TARGET description="Compile all source code" name="compile"> <!-- 指定编译后的class文件的存放位置 --> <JAVAC debug="true" destdir="."> deprecation="false" optimize="false" failοnerrοr="true"> <!-- 指定须要编译的源文件的存放位置 --> <SRC path="." /> <!-- 指定编译这些java文件须要的类库位置--> <CLASSPATH refid="classpath" /> </JAVAC> </TARGET> <!-- 运行特定的主程序 --> <TARGET description="run the main class" name="run" depends="compile"> <!-- 指定运行的主程序:lee.BeanTest。--> <JAVA failοnerrοr="true" fork="yes" classname="lee.BeanTest"> <!-- 指定运行这些java文件须要的类库位置--> <CLASSPATH refid="classpath" /> </JAVA> </TARGET> </PROJECT> |
//Axe的另外一个实现类 SteelAxe public class SteelAxe implements Axe { //默认构造器 public SteelAxe() {} //实现Axe接口的chop方法 public String chop() { return "钢斧砍柴真快"; } } |
<!-- 定义一个steelAxe bean--> <BEAN class="lee".SteelAxe id=steelAxe /> |
<REF local="”stoneAxe”/"> |
<REF local="”steelAxe”/"> |
//Chinese实现Person接口 public class Chinese implements Person { //面向Axe接口编程,而不是具体的实现类 private Axe axe; //默认的构造器 public Chinese() {} //构造注入所需的带参数的构造器 public Chinse(Axe axe) { this.axe = axe; } //实现Person接口的useAxe方法 public void useAxe() { System.out.println(axe.chop()); } } |
<!-- 下面是标准的XML文件头 --> <xml version="1.0" encoding="gb2312"?> <!-- 下面一行定义Spring的XML配置文件的dtd --> "http://www.springframework.org/dtd/spring-beans.dtd"> <!-- 以上三行对全部的Spring配置文件都是相同的 --> <!-- Spring配置文件的根元素 --> <BEANS> <!—定义第一个bean,该bean的id是chinese, class指定该bean实例的实现类 --> <BEAN class="lee".Chinese id=chinese> </BEAN> <!-- 定义stoneAxe bean --> <BEAN class="lee".SteelAxe id=steelAxe /> </BEANS> |