wrox《Expert one on one J2EE design and development》 -- Rod JohnsonSpring的优点:@低侵入式设计,代码污染极低;@独立于各种应用服务器,可以真正实现Write Once,Run Anywhere的承诺;@Spring的DI机制降低了业务对象替换的复杂性;@Spring并不完全依赖于Spring,开发者可以自由选用Spring框架的部分或全部一、Spring实现两种设计模式单态模式的示例代码: // 单态模式测试类 public class SingletonTest ... { //该类的一个普通属性 int value; //使用静态属性类保存该类的一个实例 private static SingltonTest instance; //构造器私有化,避免该类被多次实例化 private SingletonTest() ...{ System.out.println("正在执行构造器..."); } //提供静态方法来返回该类的实例 public static SingletonTest getInstance() ...{ //实例化类实例前,先检查该类的实例是否存在 if (instance == null) ...{ //如果不存在,则新建一个实例 instance = new SingletonTest(); } //返回该类的成员变量:该类的实例 return instance; } //以下提供对普通属性value的setter和getter方法 public int getValue() ...{ return value; } public void setValue(int value) ...{ this.value = value; } public static void main(String[] args) ...{ SingletonTest t1 = SingletonTest.getInstance(); SingletonTest t2 = SingletonTest.getInstance(); t2.setValue(9); System.out.println(t1 == t2); }} 二、工厂模式的回顾示例代码: // Person接口定义 public interface Person ... { /** *//** * @param name 对name打招呼 * @return 打招呼的字符串 */ public String sayHello(String name); /** *//** * @param name 对name告别 * @return 告别的字符串 */ public String sayGoodBye(String name);} // American类实现Person接口 public class American implements Person ... { public String sayHello(String name) ...{ return name + " ,Hello"; } public String sayGoodBye(String name) ...{ return name + " ,GoodBye"; }} // Chinese public class Chinese implements Person ... { public String sayHello(String name) ...{ return name + " ,您好"; } public String sayGoodBye(String name) ...{ return name + " ,再见"; }} 然后看Person工厂的代码: public class PersonFactory ... { /** *//** * 获得Person实例的实例工厂方法 * @param ethnic 调用该实例工厂方法传入的参数 * @return 返回Person实例 */ public Person getPerson(String ethnic) ...{ //根据参数返回Person接口的实例 if (ethnic.equalsIgnoreCase("china")) ...{ return new Chinese(); } else ...{ return new American(); } }} 以上是最简单的工厂模式框架,其主程序部分如下: public class FactoryTest ... { public static void main(String[] args) ...{ //创建PersonFactory的实例,获得工厂实例 PersonFactory pf = new PersonFactory(); //定义接口Person的实例,面向接口编程 Person p = null; //使用工厂获得Person的实例 p = pf.getPerson("china"); //下面是调用Person接口的方法 System.out.println(p.sayHello("prolove")); System.out.println(p.sayGoodBye("prolove")); //使用工厂获得Person的另一个实例 p = pf.getPerson("ame"); //再次调用Person接口的方法 System.out.println(p.sayHello("prolove")); System.out.println(p.sayGoodBye("prolove")); }} 三、Spring对单态与工厂模式的实现下面是关于两个实例的配置文件: <? xml version = " 1.0 " encoding = " gb2312 " ?> <! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " > < beans > < bean id = " chinese " class = " prolove.Chinese " /> < bean id = " american " class = " prolove.American " /> </ beans > 主程序部分如下: public class SpringTest ... { public static void main(String[] args) ...{ //实例化Spring容器 ApplicationContext ctx = new FileSystemXmlApplicationContext ("bean.xml"); //定义Person接口的实例 Person p = null; //通过Spring上下文获得chinese实例 p = (Person)ctx.getBean("chinese"); //执行chinese实例的方法 System.out.println(p.sayHello("prolove")); System.out.println(p.sayGoodBye("prolove")); p = (Person)ctx.getBean("american"); System.out.println(p.sayHello("prolove")); System.out.println(p.sayGoodBye("prolove")); }} 使用Spring时,即使没有工厂类PersonFactory,程序一样可以使用工厂模式,Spring完全可以提供所有工厂模式的功能下面对主程序部分进行简单的修改: public class SpringTest ... { public static void main(String[] args) ...{ //实例化Spring容器 ApplicationContext ctx = new FileSystemXmlApplicationContext ("bean.xml"); //定义Person接口的实例p1 Person p1 = null; //通过Spring上下文获得chinese实例 p1 = (Person)ctx.getBean("chinese"); //定义Person接口的实例p2 Person p2 = null; //通过Spring上下文获得american实例 p2 = (Person)ctx.getBean("american"); System.out.println(p1 == p2); }}