Spring Bean工厂创建Bean实例的方法之使用静态工厂类
来源:互联网 发布:中国网络作家村 编辑:程序博客网 时间:2024/04/30 07:25
定义接口:
package Bean.staticFactoryBean;
public interface Axe ...{
public String chop();
}
package Bean.staticFactoryBean;
public interface Person ...{
public void useAxe();
}
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 "这是一把木头斧子";
}
}
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();
}
}
}
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>
<!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();
}
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();
}
- Spring Bean工厂创建Bean实例的方法之使用静态工厂类
- Spring Bean工厂创建Bean实例的方法之使用工厂类
- Spring使用静态工厂方法创建Bean
- Spring使用静态工厂方法创建Bean
- Spring Bean工厂创建Bean实例的方法之使用构造器
- Spring学习之使用静态工厂方法以及实例工厂方法配置bean
- Spring:Spring使用静态工厂方法创建Bean
- 【spring notes】 使用静态工厂方法创建Bean
- 转:Spring使用静态工厂方法创建Bean
- Spring使用静态工厂方法创建Bean(一)
- Spring使用实例工厂方法创建Bean(一)
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- 通过Bean工厂创建bean实例的几种方法
- spring学习小结7_使用工厂方法来配置bean:静态工厂方法和实例工厂方法
- spring 9 bean配置--通过工厂方法(静态工厂方法&实例工厂方法
- 学习Spring(二) 调用静态工厂方法创建Bean
- Spring 使用静态工厂方式实例化Bean
- 实例化创建bean 和静态工厂创建bean
- c#学习笔记(四) get-set
- c#学习笔记(五)struct
- 笑话100则……确实把我笑坏了!
- Apache、php、mysql在windows下的安装与配置图解(最新版)
- (帅)如何利用VNC远程图形界面控制Linux
- Spring Bean工厂创建Bean实例的方法之使用静态工厂类
- xml data model
- 三表联合的SQL语句
- 用Apache Axis 实现Web Service(三)
- 键盘键与虚拟键码对照表
- Linux下Oracle安装配置宝典
- 如何编写线程安全的DLL
- 程序设计语言的谱系
- 你讨厌 C++中的“<<” 了吗?