读书人

怎么使用bboss aop框架的工厂模式实现

发布时间: 2012-08-24 10:00:21 作者: rapoo

如何使用bboss aop框架的工厂模式实现组件管理
1.bboss aop框架的工厂模式实现组件管理概述


bboss aop框架的工厂模式是bbossgroups3.0新引入的一种组件创建机制,是对原有的属性注入模式和构造函数注入模式的很好补充。具体实现方式如下,在property元素上增加factory-bean、factory-class和factory-method三个属性,也就是对应property元素管理的组件实例通过factory-bean或者factory-class对应的组件中factory-method对应的方法来创建。factory-bean和factory-class的主要区别:
factory-bean 指定的值是一个组件id,对应aop组件配置文件中的其他组件的一个引用,同时factory-method指定的方法名称是这个组件的
一个实例方法,aop框架通过调用factory-bean对应的组件的factory-method对应的方法来创建组件实例。
factory-class 指定的是创建组件的工厂实现类的全路径,同时factory-method指定的方法名称是这个组件的一个静态方法,aop框架通过调用factory-class对应的组件的factory-method对应的方法来创建组件实例。
如果factory-method指定的方法带有参数,可以通过construction节点包含property节点的方式来指定,多个property节点的排列顺序
要和方法参数的顺序一致,类型要可以转换。

具体的实例可以参考测试用例:
/bbossaop/test/org/frameworkset/spi/beans/factory
我们通过一个简单的案例来介绍工厂模式的使用方法,需要创建的组件为:
org.frameworkset.spi.beans.factory.TestBean
我们通过分别:
通过工厂组件org.frameworkset.spi.beans.factory.TestFactoryBeanCreate中的相关方法来创建TestBean的实例
通过工厂类org.frameworkset.spi.beans.factory.StaticBeanFactory中的相关静态方法来创建TestBean的实例。

下面介绍具体的程序和配置
2.配置文件中如何配置工厂模式

<!-- Warning: Factory pattern can not against loop ioc,while loop ioc occured , unknown exception will be throwed. --><properties><!--This is a webservice client example.ApplicationContext context = ApplicationContext.getApplicationContext("org/frameworkset/spi/beans/factory/factorybean.xml");ETLWebService etlWebService = (ETLWebService)context.getBeanObject("ETLWebService");--><property name="ETLWebService" factory-bean="ETLWebServiceFactory" factory-method="create"/><property name="ETLWebServiceFactory" value="org.frameworkset.monitor.etl.webservice.ETLWebService"/><property name="address" value="http://localhost/kettle/webservice/ETLWebServicePort"/>      </property><!-- bean [testbean] will be created used facctory method [createNoArgs] of factory bean [testfactorybean].testfactorybean is defined by another composinent. --><property name="testbeanCreateNoArgs" factory-bean="testfactorybean" factory-method="createNoArgs"/><!-- bean [testbean] will be created used facctory method [createWithArgs] of factory bean [testfactorybean].testfactorybean is defined by another component.constructions parameters will be used as the parameters of createWithArgs method. --><property name="testbeanCreateByArgs" factory-bean="testfactorybean" factory-method="createWithArgs"><construction><property name="name" value="duoduo"/><property name="id" value="12"/></construction></property><!-- bean [testbean] will be created used facctory method [createNoArgsThrowException] of factory bean [testfactorybean].testfactorybean is defined by another component. --><property name="testbeanCreateNoArgsThrowException" factory-bean="testfactorybean" factory-method="createNoArgsThrowException"/><!-- bean [testbean] will be created used factory method [createWithArgsThrowException] of factory bean [testfactorybean].testfactorybean is defined by another component.constructions parameters will be used as the parameters of createWithArgsThrowException method.A Exception occur when call createWithArgsThrowException method and component created failed. --><property name="testbeanCreateByArgsThrowException" factory-bean="testfactorybean" factory-method="createWithArgsThrowException"><construction><property name="name" value="duoduo"/><property name="id" value="12"/></construction></property><property name="testfactorybean" value="duoduo"/><property name="factorydata2" value="12"/>      </property><!-- createWithArgs method must be a static method of factory class org.frameworkset.spi.beans.factory.TestFactoryBeanCreate,  constructions parameters will be used as the parameters of createWithArgs method.--><property name="staticTestbeanCreateByArgs" factory-factory-method="createWithArgs"><construction><property name="name" value="duoduo"/><property name="id" value="12"/></construction></property><!-- createNoArgs method must be a static method of factory class org.frameworkset.spi.beans.factory.TestFactoryBeanCreate --><property name="staticTestbeanCreateNoArgs" factory-factory-method="createNoArgs"/><!-- createWithArgsThrowException method must be a static method of factory class org.frameworkset.spi.beans.factory.TestFactoryBeanCreate,  constructions parameters will be used as the parameters of createWithArgsThrowException method. A Exception occur when call createWithArgsThrowException method and component created failed. --><property name="staticTestbeanCreateByArgsThrowException" factory-factory-method="createWithArgsThrowException"><construction><property name="name" value="duoduo"/><property name="id" value="12"/></construction></property><!-- createNoArgsThrowException method must be a static method of factory class org.frameworkset.spi.beans.factory.TestFactoryBeanCreateA Exception occur when call createNoArgsThrowException method and component created failed. --><property name="staticTestbeanCreateNoArgsThrowException" factory-factory-method="createNoArgsThrowException"/></properties>

对应的配置文件存放的地址:org/frameworkset/spi/beans/factory/factorybean.xml
3.需要创建的组件
package org.frameworkset.spi.beans.factory;/** * <p>Title: TestBean.java</p>  * <p>Description: </p> * <p>bboss workgroup</p> * <p>Copyright (c) 2007</p> * @Date 2011-1-14 上午10:13:42 * @author biaoping.yin * @version 1.0 */public class TestBean {private String name;private int id;public TestBean(String name, int id) {this.name = name;this.id = id;}public TestBean() {}protected String getName() {return name;}protected void setName(String name) {this.name = name;}protected int getId() {return id;}protected void setId(int id) {this.id = id;}}


4.管理组件的工厂
静态工厂

package org.frameworkset.spi.beans.factory;/** * <p>Title: StaticBeanFactory.java</p>  * <p>Description: </p> * <p>bboss workgroup</p> * <p>Copyright (c) 2007</p> * @Date 2011-1-14 上午10:55:07 * @author biaoping.yin * @version 1.0 */public class StaticBeanFactory {public static TestBean createWithArgs(String name,int id){return new TestBean(name,id);}public static TestBean createNoArgs(){return new TestBean();}public static TestBean createWithArgsThrowException(String name,int id) throws Exception{throw new Exception("createWithArgsThrowException name:"+name+",id:" + id );}public static TestBean createNoArgsThrowException() throws Exception{throw new Exception("createWithArgsThrowException()");}}


组件工厂
package org.frameworkset.spi.beans.factory;/** * <p>Title: TestFactoryBeanCreate.java</p>  * <p>Description: </p> * <p>bboss workgroup</p> * <p>Copyright (c) 2007</p> * @Date 2011-1-14 上午10:13:31 * @author biaoping.yin * @version 1.0 */public class TestFactoryBeanCreate {private String factorydata1;private String factorydata2;public TestBean createNoArgs(){return new TestBean();}public TestBean createWithArgs(String name,int id){return new TestBean( name,id);}public TestBean createNoArgsThrowException() throws Exception{throw new Exception("createNoArgsThrowException " );}public TestBean createWithArgsThrowException(String name,int id) throws Exception{throw new Exception("createWithArgsThrowException name:"+name+",id:" + id );}public String getFactorydata1() {return factorydata1;}public void setFactorydata1(String factorydata1) {this.factorydata1 = factorydata1;}public String getFactorydata2() {return factorydata2;}public void setFactorydata2(String factorydata2) {this.factorydata2 = factorydata2;}}


5.测试用例
package org.frameworkset.spi.beans.factory;import org.frameworkset.spi.ApplicationContext;import org.junit.Before;import org.junit.Test;/** * <p>Title: TestCase.java</p>  * <p>Description: </p> * <p>bboss workgroup</p> * <p>Copyright (c) 2007</p> * @Date 2011-1-14 上午11:16:36 * @author biaoping.yin * @version 1.0 */public class TestCase {ApplicationContext context ;@Beforepublic void initContext(){context = ApplicationContext.getApplicationContext("org/frameworkset/spi/beans/factory/factorybean.xml");}@Testpublic void runCase(){TestBean bean =  (TestBean) context.getBeanObject("testbeanCreateNoArgs");TestBean bean1 =  (TestBean) context.getBeanObject("testbeanCreateByArgs");TestBean bean2 =  (TestBean) context.getBeanObject("staticTestbeanCreateByArgs");TestBean bean3 =  (TestBean) context.getBeanObject("staticTestbeanCreateNoArgs");}@Testpublic void runExceptionCase(){try {TestBean bean = (TestBean) context.getBeanObject("testbeanCreateNoArgsThrowException");} catch (Exception e) {e.printStackTrace();}try {TestBean bean1 = (TestBean) context.getBeanObject("testbeanCreateByArgsThrowException");} catch (Exception e) {e.printStackTrace();}try {TestBean bean2 = (TestBean) context.getBeanObject("staticTestbeanCreateByArgsThrowException");} catch (Exception e) {e.printStackTrace();}try {TestBean bean3 = (TestBean) context.getBeanObject("staticTestbeanCreateNoArgsThrowException");} catch (Exception e) {e.printStackTrace();}}}


有点想用你的框架来试试,只是以前用过一些国内开发的框架,一般都是过一段时间后,就不再开发了,还有就是里面有bug的,或自己框架的缺陷从来不说。搞得花费了大量的时间和精力后,却不了了之,实在让人伤心。

说上面的话,不是贬低国内人员开发的框架,只是希望你不要象他们那样。一定要坚持下去。

有点想用你的框架来试试,只是以前用过一些国内开发的框架,一般都是过一段时间后,就不再开发了,还有就是里面有bug的,或自己框架的缺陷从来不说。搞得花费了大量的时间和精力后,却不了了之,实在让人伤心。

说上面的话,不是贬低国内人员开发的框架,只是希望你不要象他们那样。一定要坚持下去。

国人的开源,只要坚持,还是可以做的很好的,我一直在关注bboss,他的作者,非常不错,持续下去,这种精神就是开源,我们要支持。
我也看到过很多国人的开源,后来不了了之,这和整个环境有关,因为更多对开源只是索取,还有很多人持着不信任,或无端的挑剔。但还是有很多优秀的开源,不断充实着我们的开源世界。从我们自身开始,肯定和鼓励我们开源作者的努力,他们无私的奉献。我相信国人的开源“明天更美好”。

支持你yin_bp,有个小建议,是否可以考虑让更多人参与到你的项目中,找一些志同道合的人,集众人之智慧,持续发展!

个人愚见。

4 楼 yin_bp 2011-04-30 呵呵,谢谢大家建议,也很希望有兴趣的朋友能够参与到bboss项目的建设中来,集思广益,使得该项目确确实实能够为大家所用,为大家所接纳,也算是国人为国人做点还算得上主流的开发框架,呵呵,再次感谢大家,也再次欢迎大家加入bboss项目的开发团队(不好意思,到现在为止,团队成员只有我一个人,编码,文档,测试,版本维护,呵呵,而且全部是业余时间在搞) 5 楼 yin_bp 2011-04-30 框架整个发展过程始终和实际项目结合在一起,框架构建的指导思想:
在项目中提炼框架、在项目中完善框架、与项目相互促进形成良性互动。
为了正确引导开发人员使用框架,编写框架组件api文档、测试用例和使用demo。
6 楼 binsoft 2011-09-06 说句心里话,我觉得LZ应该从底层的实现开始写一份完整的文档,包—EMO以及用法,还有代码中的注释也应该多完善一下,还有一些依赖的JS文件感觉有点乱;希望你能够坚持下去,不要放弃。 7 楼 yin_bp 2011-09-06 binsoft 写道说句心里话,我觉得LZ应该从底层的实现开始写一份完整的文档,包—EMO以及用法,还有代码中的注释也应该多完善一下,还有一些依赖的JS文件感觉有点乱;希望你能够坚持下去,不要放弃。
一直在努力,一直在坚持,谢谢大家的建议和支持,bbossgroup会持续改进的,文档也会不断完善的,框架本身依赖的js不多,主要是demo里面有些效果需要用到一些js,也没空整理,框架依赖的资源可以参考文档《bbossgroups框架发布的jar包和依赖jar包概览
》:
http://yin-bp.iteye.com/blog/1143994

读书人网 >操作系统

热点推荐