标签:
package test;publicinterfacePerson{publicvoid sayHello();publicvoid sayBye();}//中国人package test;publicclassChineseimplementsPerson{publicvoid sayBye(){System.out.println("再见");}publicvoid sayHello(){System.out.println("你好");}}//美国人package test;publicclassAmericanimplementsPerson{publicvoid sayBye(){System.out.println("Bye");}publicvoid sayHello(){System.out.println("hello");}}<?xml version="1.0" encoding="gb2312"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.sprintframework.org/dtd/spring-beans.dtd"><beans><beanid="chinese"class="test.Chinese"/><beanid="american"class="test.American"/></beans><beansxmlns="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"><beanid="chinese"class="test.Chinese"/><beanid="american"class="test.American"/></beans>package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class Lantch { public static void main(String[] args) throws Exception { new Lantch().doIt(); } public void doIt() { /* 获取bean映射配置文件,配置文件放于与当前测试类在同一个目录下 */ ApplicationContext ctx = new FileSystemXmlApplicationContext(getClass().getResource("bean.xml").toString()); /* 配置文件放在 resources目录下面 */ // ApplicationContext ctx = new ClassPathXmlApplicationContext("MATE-INF/bean.xml"); Person person = null; person = (Person) ctx.getBean("chinese"); person.sayHello(); person.sayBye(); person = (Person) ctx.getBean("american"); person.sayHello(); person.sayBye(); }}package test2;publicinterfacePerson{publicvoid useAxe();}package test2;publicinterfaceAxe{publicvoid chop();}package test2;publicclassChineseimplementsPerson{/* 默认无参构造方法不管为私的还是公有的,都可以访问,并且要保证bean中存在可以被外界访问的无参构造函数 */privateChinese(){};/* 定义需要被使用的斧头接口,具体使用什么斧头这里不管 */privateAxe axe;/* * 定义被注入斧头的set方法,该方法一定要符合JAVABEAN的标准。在运行时候,Sping就会根据配置的<ref * local=""/>,找到需要注入的实现类 */publicvoid setAxe(Axe axe){this.axe = axe;}/* 这个时候使用的axe,就不再是接口Axe本身,而是被注入的子类实例,所以这里的chop()动作就是具体子类的chop动作 */publicvoid useAxe(){ axe.chop();}}package test2;publicclassStoneAxeimplementsAxe{publicvoid chop(){System.out.println("石斧慢慢砍");}}<?xml version="1.0" encoding="gb2312"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.sprintframework.org/dtd/spring-beans.dtd"><beans><beanid="chinese"class="test2.Chinese"><!-- 声明实现类test2.Chinese中的属性 --><propertyname="axe"><!-- 指定其中声明的属性,需要用本地的那个id对应的class 这里local的值为"stoneAxe",表示axe的属性值在注入的时候, 将会用test2.StoneAxe实例注入,到时在实例类Chinese中使用 axe的时候,实际上调用的时候StoneAxe的实例 --><reflocal="stoneAxe"/></property></bean><beanid="stoneAxe"class="test2.StoneAxe"/></beans>package test2;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;publicclassCaller{publicstaticvoid main(String[] args){Caller caller =newCaller(); caller.doIt();}publicvoid doIt(){String bean = getClass().getResource("bean.xml").toString();ApplicationContext ctx =newFileSystemXmlApplicationContext(bean);Person person =(Person) ctx.getBean("chinese"); person.useAxe();}}package test2;publicclassChineseimplementsPerson{/* 默认无参构造方法不管为私的还是公有的,都可以访问,并且要保证bean中存在可以被外界访问的无参构造函数 */privateChinese(){};/* 定义需要被使用的斧头接口,具体使用什么斧头这里不管 */privateAxe axe;publicChinese(Axe axe){this.axe = axe;}/* 这个时候使用的axe,就不再是接口Axe本身,而是被注入的子类实例,所以这里的chop()动作就是具体子类的chop动作 */publicvoid useAxe(){ axe.chop();}}<?xml version="1.0" encoding="gb2312"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.sprintframework.org/dtd/spring-beans.dtd"><beans><bean id="chinese"class="test2.Chinese"><!--定义需要被构造注入的实现类,同设值注入的结果一样,都是注入接口的实现类--><constructor-arg><ref bean="stoneAxe"/></constructor-arg></bean><bean id="stoneAxe"class="test2.StoneAxe"/></beans> <properties><spring.version>3.0.5.RELEASE</spring.version></properties><dependencies><!-- Spring 3 dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency></dependencies>标签:
原文地址:http://www.cnblogs.com/liu-qing/p/4540171.html