<?xml version="1.0" encoding="GBK"?> <project name="spring" basedir="." default=""> <property name="src" value="src"/> <property name="dest" value="classes"/> <path id="classpath"> <fileset dir="../../lib"> <include name="**/*.jar"/> </fileset> <pathelement path="${dest}"/> </path> <target name="compile" description="Compile all source code"> <delete dir="${dest}"/> <mkdir dir="${dest}"/> <copy todir="${dest}"> <fileset dir="${src}"> <exclude name="**/*.java"/> </fileset> </copy> <javac destdir="${dest}" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <src path="${src}"/> <classpath refid="classpath"/> <compilerarg value="-Xlint:deprecation"/> </javac> </target> <target name="run" description="Run the main class" depends="compile"> <java classname="lee.SpringTest" fork="yes" failonerror="true"> <classpath refid="classpath"/> </java> </target> </project>
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置工厂Bean,该Bean负责产生其他Bean实例 --> <bean id="personFactory" class="org.crazyit.app.factory.PersonFactory"/> <!-- 下面配置驱动Spring调用personFactory Bean的getPerson()方法来创建Bean 该bean元素包含的constructor-arg元素用于为工厂方法指定参数, 因此这段配置会驱动Spring以反射方式来执行如下代码: PersonFactory pf = container.get("personFactory"); // container代表Spring容器 chinese = pf.getPerson("chin"); --> <bean id="chinese" factory-bean="personFactory" factory-method="getPerson"> <!-- 配置实例工厂方法的参数 --> <constructor-arg value="chin"/> </bean> <!-- 下面配置会驱动Spring以反射方式来执行如下代码: PersonFactory pf = container.get("personFactory"); // container代表Spring容器 american = pf.getPerson("ame"); --> <bean id="american" factory-bean="personFactory" factory-method="getPerson"> <constructor-arg value="ame"/> </bean> </beans>
package org.crazyit.app.factory; import org.crazyit.app.service.*; import org.crazyit.app.service.impl.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class PersonFactory { // 获得Person实例的实例工厂方法 // ethnic参数决定返回哪个Person实现类的实例 public Person getPerson(String ethnic) { if (ethnic.equalsIgnoreCase("chin")) { return new Chinese(); } else { return new American(); } } }
package org.crazyit.app.service; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public interface Person { // 定义一个打招呼的方法 public String sayHello(String name); // 定义一个告别的方法 public String sayGoodBye(String name); }
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class Chinese implements Person { // 实现Person接口必须实现如下两个方法 public String sayHello(String name) { return name + ",您好"; } public String sayGoodBye(String name) { return name + ",下次再见"; } }
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class American implements Person { // 实现Person接口必须实现如下两个方法 public String sayHello(String name) { return name + ",Hello!"; } public String sayGoodBye(String name) { return name + ",Good Bye!"; } }
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class SpringTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person p1 = ctx.getBean("chinese" , Person.class); System.out.println(p1.sayHello("Mary")); System.out.println(p1.sayGoodBye("Mary")); Person p2 = ctx.getBean("american" , Person.class); System.out.println(p2.sayHello("Jack")); System.out.println(p2.sayGoodBye("Jack")); } }
来源:https://www.cnblogs.com/tszr/p/12370305.html