使用FactoryBean接口简化工厂Bean开发
来源:互联网 发布:win7系统玩游戏优化 编辑:程序博客网 时间:2024/06/05 16:04
使用FactoryBean接口简化工厂Bean开发,但是,一个工厂只能有一类产品
public class PersonFactory implements FactoryBean
...{
Person p = null;
//返回产品
public Object getObject() throws Exception
...{
if (p == null)
...{
p = new Chinese();
}
return p;
}
//返回产品类型
public Class getObjectType()
...{
return Chinese.class;
}
//产生的实例是否为单态
public boolean isSingleton()
...{
return true;
}
}
...{
Person p = null;
//返回产品
public Object getObject() throws Exception
...{
if (p == null)
...{
p = new Chinese();
}
return p;
}
//返回产品类型
public Class getObjectType()
...{
return Chinese.class;
}
//产生的实例是否为单态
public boolean isSingleton()
...{
return true;
}
}
配置文件:
<beans>
<bean id="beanfactory" class="Bean.Beanfactory.PersonFactory">
</bean>
</beans>
这样,根据beanfactory获得的bean不再是PersonFactory,而是其产品Chinese,如果需要得到PersonFactory实例,有另外一种调用方式,如下:
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+"/beanfactory.xml");
Chinese p=(Chinese)context.getBean("beanfactory");
System.out.println(context.getBean("&beanfactory"));
System.out.println(p);
}
其中System.out.println(context.getBean("&beanfactory"));返回工厂的实例
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/beanfactory.xml");
Chinese p=(Chinese)context.getBean("beanfactory");
System.out.println(context.getBean("&beanfactory"));
System.out.println(p);
}
其中System.out.println(context.getBean("&beanfactory"));返回工厂的实例
运行结果:
Bean.Beanfactory.PersonFactory@ca470
Bean.Beanfactory.Chinese@1ffc686
- 使用FactoryBean接口简化工厂Bean开发
- 使用FactoryBean接口简化工厂Bean开发
- FactoryBean工厂Bean
- 使用FactoryBean接口实现自定义bean初始化
- 使用FactoryBean接口实现自定义bean初始化
- Spring FactoryBean接口使用
- 【Spring】使用FactoryBean配置Bean
- Spring4笔记----实现 FactoryBean 接口配置 Bean
- (Spring-bean)生命周期接口、FactoryBean、BeanFactory交互、子bean
- 使用JSR303和AOP简化接口开发
- spring 静态工厂 &实例工厂实例方法 & 实现factorybean接口
- Spring配置bean的方法(工厂方法和Factorybean)
- spring详解:通过FactoryBean自定义工厂初始化Bean
- spring通过工厂方法和FactoryBean来配置Bean
- Spring配置bean的方法(工厂方法和Factorybean)
- Spring-12,通过工厂方法和FactoryBean配置bean
- 【Spring:FactoryBean接口】实现FactoryBean接口,Spring在初始化bean时有何不同
- Spring 如何将无法使用new操作符新建的对象作为spring的bean (使用FactoryBean接口)
- linux as 4安装好10G后却使用不了
- C++内存泄漏——浅谈C/C++内存泄漏及其检测工具
- 上班人员必读:“五险一金”详解!
- 用Eclipse3.2 + Myeclipse5.0GA + Tomcat5.5.17 + j2sdk1.5 搭建J2EE开发环境
- 数据库设计三大范式应用实例剖析
- 使用FactoryBean接口简化工厂Bean开发
- C++内存泄漏——C++中动态内存分配引发问题的解决方案(1)
- SQL Select Into
- 正则表达式30分钟入门教程 v2.1
- 取随机数时的小技巧
- java字符编码原理解析
- C#中的"序列化" Serializable 理解
- 加速你的Eclipse
- Java与.NET 的Web Services相互调用