定义接口: package Bean.staticFactoryBean; public interface Axe ... { public String chop();} package Bean.staticFactoryBean; public interface Person ... { public void useAxe();} 实现类: package Bean.staticFactoryBean; public class AmericanPerson implements Person ... { private Axe axe; public Axe getAxe() ...{ return axe; } public void setAxe(Axe axe) ...{ this.axe = axe; } public void useAxe() ...{ System.out.println(axe.chop()); }} package Bean.staticFactoryBean; public class ChinesePerson implements Person ... { private Axe axe; public Axe getAxe() ...{ return axe; } public void setAxe(Axe axe) ...{ this.axe = axe; } public void useAxe() ...{ System.out.println(axe.chop()); }} package Bean.staticFactoryBean; public class SteelAxe implements Axe ... { public String chop() ...{ return "这是一把铁斧子"; }} package Bean.staticFactoryBean; public class WoodAxe implements Axe ... { public String chop() ...{ return "这是一把木头斧子"; }} 工厂类: package Bean.staticFactoryBean; import Bean.staticFactoryBean. * ; public class BeingFactory ... { public static Person getPerson(String arg)...{ if(arg.equalsIgnoreCase("chinese"))...{ return new Bean.staticFactoryBean.ChinesePerson(); } else...{ return new Bean.staticFactoryBean.AmericanPerson(); } }} 配置文件,和使用构造函数创建bean不同的是,这里的class是静态方法的实现类,多加了factory-method属性,工厂方法的参数通过<constructor-arg>定义 <? xml version="1.0" encoding="UTF-8" ?> <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" > < beans > < bean id ="chinese" class ="Bean.staticFactoryBean.BeingFactory" factory-method ="getPerson" > < constructor-arg > < value > chinese </ value > </ constructor-arg > < property name ="axe" > < ref local ="woodaxe" /> </ property > </ bean > < bean id ="american" class ="Bean.staticFactoryBean.BeingFactory" factory-method ="getPerson" > < constructor-arg > < value > american </ value > </ constructor-arg > < property name ="axe" > < ref local ="steelaxe" /> </ property > </ bean > < bean id ="woodaxe" class ="Bean.staticFactoryBean.WoodAxe" ></ bean > < bean id ="steelaxe" class ="Bean.staticFactoryBean.SteelAxe" ></ bean > </ beans > 测试代码: public static void main(String[] args) throws Exception ... { String path=new Test().getClass().getResource("/").getPath(); String realpath=path.substring(1, path.length()); ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/staticMethodbeans.xml"); Person person1=(Person)context.getBean("chinese"); person1.useAxe(); Person person2=(Person)context.getBean("american"); person2.useAxe(); }