在“JBoss Seam+ExtJS”项目中使用Groovy代替Java

阅读了《Groovy In Action》一书,做了网上调研,经历了最初的FUD(Fear, uncertainty, doubt )过程,最终决定使用Groovy代替Java来写JBoss Seam组件。

 

本文是近一个月来, 使用Groovy编程的几点粗浅的体会。最后展示了一些Groovy源代码。

 

1.好处

 

(1)代码变得非常简洁,能够直接把程序的意图表现出来。同样的功能,很难想象再回到Java去编程。

(2)闭包(Closure)的使用,大大简化了某些算法,尤其是集合操作,开发效率大为提高。

(3)可以用JavaScript的感觉去写Groovy程序,因为两者都属于动态类型语言。

 

2.不便之处

 

(1)Groovy Eclipse Plugin的当前版本(2008-01-03发表)的编译速度还比较慢,功能上太弱。希望在未来的几个月中,这种情况能得到改变。

(2)编译阶段,对方法名引用错误,或者方法传递参数的错误,不再提示,要等到运行时才能发现。

(3)GlassFish中检测不到Groovy编写的类的修改,无法获得自动加载的热部署好处。 

 

3.不确定之处

 

(1)是否会影响到整个后台程序的运行速度?

(2)是否能被开发团队中其他成员接受和欢迎? 

 

4.Groovy代码展示1:Hibernate实体类

package com.divo.app.domain

import javax.persistence.*
import org.jboss.seam.annotations.*

//网格视图-用户版本
@Entity
@Name("app.myGridView")
@Table(name="app_MyGridView",uniqueConstraints = [
   @UniqueConstraint(columnNames =
   ["viewId", "userId"] )
])
class MyGridView {
    
    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE, generator="appNextId")
    @TableGenerator(name="appNextId", table="app_NextId", allocationSize=1)
    Long id
    
    @Column(nullable=false)
    Long userId
    
    @Column(nullable=false)
    Long viewId
    
    @Column(nullable=false,length=4000)
    String viewState
	
}
//EOP

 

 

5.Groovy代码展示2:作为JBoss Seam组件的服务类

package com.divo.app.service

import org.hibernate.*

import org.jboss.seam.annotations.*
import org.jboss.seam.security.*

import com.divo.app.domain.*
import com.divo.core.misc.*
import com.divo.core.util.*

@Name("app.appInfoService")
@AutoCreate
class AppInfoService extends BaseService {

    String appInfoFileName
    
    def getAppInfo() {
	    def dir = appInfoFileName?"":AppUtil.getRootDir() 
	
	    def s = Constants.FILE_SEPARATOR
	    def fileName = appInfoFileName?appInfoFileName:"app${s}version.txt"
	    def file = new File(dir+fileName)
	    def appInfo = new AppInfo() 
		
	    def count = 0
	    file.splitEachLine(/\,/){
		    if (it.size()>1 && count>0) {
				appInfo.version = it[0]
				appInfo.buildId = it[1]
				appInfo.copyright = it[2]
				appInfo.productName = it[3]
				appInfo.isDebug = (it[6]=="Y" || it[6]=="y")
		    }
		    count++
	    }	    
	   appInfo.currentDate = DateTimeUtil.getNowDate()
	   appInfo.contextPath = appInfoFileName?"":AppUtil.getContextPath() 
	   appInfo
    }
    
}
//EOP
  

6.Groovy代码展示3:单元测试类

 

package com.divo.app.service.test

import org.testng.annotations.*
import com.divo.app.service.*
import com.divo.core.util.*
import com.divo.core.misc.*

class AppInfoServiceTest extends AppTestCase {

    def _service
    
    @BeforeClass
    @Override
    void init() {
        super.init()
        
        _service = new AppInfoService()
    }
   
    @BeforeMethod
    @Override
    void begin() {
        super.begin()
        
        _service.setSession(session)
    }
    
    @Test 
    void canGetAppInfo() {
	    _service.appInfoFileName = "test//version.txt"  
	    def appInfo = _service.getAppInfo()
	    def currentDate = DateTimeUtil.getNowDate()	    
	    
	    assert appInfo.version=="2.0.0-b1"
	    assert appInfo.buildId=="2008020112"
	    assert appInfo.copyright=="fzx"
	    assert appInfo.productName=="软件问题跟踪系统"
	    assert appInfo.currentDate==currentDate
    }
    
}
//EOP
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值