package com.spring.test;public interface IHelloMessage { public String sayHello();}
package com.spring.test;public class HelloChina implements IHelloMessage { public String sayHello() { // TODO Auto-generated method stub return "你好中国!"; }}
package com.spring.test;public class HelloWorld implements IHelloMessage { public String sayHello() { // TODO Auto-generated method stub return "Hello World!"; }}
package com.spring.test;public class Person { private IHelloMessage helloMessage; public IHelloMessage getHelloMessage() { return helloMessage; } public void setHelloMessage(IHelloMessage helloMessage) { this.helloMessage = helloMessage; } public String sayHello() { // TODO Auto-generated method stub return this.helloMessage.sayHello(); }}
package com.spring.test;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;public class Main { /** * @param args */ public static void main(String[] args) { Resource resource = new FileSystemResource("helloMessage.xml"); @SuppressWarnings("deprecation") BeanFactory factory = new XmlBeanFactory(resource); Person person = (Person) factory.getBean("person"); String string = person.sayHello(); System.out.println("please to say" + string); }}