<?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"> <!-- 下面配置驱动Spring调用BeingFactory的静态getBeing()方法来创建Bean 该bean元素包含的constructor-arg元素用于为静态工厂方法指定参数, 因此这段配置会驱动Spring以反射方式来执行如下代码: dog = org.crazyit.app.factory.BeingFactory.getBeing("dog"); --> <bean id="dog" class="org.crazyit.app.factory.BeingFactory" factory-method="getBeing"> <!-- 配置静态工厂方法的参数 --> <constructor-arg value="dog"/> <!-- 驱动Spring以"我是狗"为参数来执行dog的setMsg()方法 --> <property name="msg" value="我是狗"/> </bean> <!-- 下面配置会驱动Spring以反射方式来执行如下代码: dog = org.crazyit.app.factory.BeingFactory.getBeing("cat"); --> <bean id="cat" class="org.crazyit.app.factory.BeingFactory" factory-method="getBeing"> <!-- 配置静态工厂方法的参数 --> <constructor-arg value="cat"/> <!-- 驱动Spring以"我是猫"为参数来执行dog的setMsg()方法 --> <property name="msg" value="我是猫"/> </bean> </beans>
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) { // 以类加载路径下的配置文件创建ClassPathResource实例 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Being b1 = ctx.getBean("dog" , Being.class); b1.testBeing(); Being b2 = ctx.getBean("cat" , Being.class); b2.testBeing(); } }
package org.crazyit.app.factory; import 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 BeingFactory { // 返回Being实例的静态工厂方法 // arg参数决定返回哪个Being类的实例 public static Being getBeing(String arg) { // 调用此静态方法的参数为dog,则返回Dog实例 if (arg.equalsIgnoreCase("dog")) { return new Dog(); } // 否则返回Cat实例 else { return new Cat(); } } }
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 Being { public void testBeing(); }
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 Cat implements Being { private String msg; // msg的setter方法 public void setMsg(String msg) { this.msg = msg; } // 实现接口必须实现的testBeing方法 public void testBeing() { System.out.println(msg + ",猫喜欢吃老鼠"); } }
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 Dog implements Being { private String msg; // msg的setter方法 public void setMsg(String msg) { this.msg = msg; } // 实现接口必须实现的testBeing()方法 public void testBeing() { System.out.println(msg + ",狗爱啃骨头"); } }
来源:https://www.cnblogs.com/tszr/p/12370306.html