一个简单的接口:
public interface ChangLetter { public String change();}
两个实现:
package com.pas.service;import com.inter.ChangLetter;public class LowLetter implements ChangLetter { private String str; @Override public String change() { // 大写-》小写 return str.toLowerCase(); } public String getStr() { return str; } public void setStr(String str) { this.str = str; }}
package com.pas.service;import com.inter.ChangLetter;public class UpLetter implements ChangLetter { private String str; @Override public String change() { // 小写-》大写 return str.toUpperCase(); } public String getStr() { return str; } public void setStr(String str) { this.str = str; }}
spring配置文件:
使用模拟view:
public class Test { public static void main(String[] args) { ApplicationContext ac= new ClassPathXmlApplicationContext("com/pas/service/beans.xml"); ChangLetter change= (ChangLetter) ac.getBean("changeLetter"); System.out.println(change.change()); }}
重新配置Spring配置文件 代码无需更改