Chen Jian

[Rod Johnson] 关于Checked Exception和RuntimeException

基本上,Rod Johnson 对Checked Exception没有好感,一个原因是降低代码可读性,另一个原因是大部分针对Checked Exception的catch都没有用处。 SUN认为,“checked exceptions should be the norm. Runtime exceptions indicate programming errors.” Rod说他并不完全买账,随后列举了自己的一些实践。但我觉得,他自己的实践,跟SUN的官方意见基本上是一致的: Checked Exception应该代表一种业务异常,可以视为一个方法的“第二输出”。一个典型的例子就是“系统检测到取款金额大于帐户余额”。 而Runtime Exception 应该用于代表非业务的、并且Caller没办法对付的异常。举例来说,如果一个方法检测到取款金额大于帐户余额,这个方法的调用者就应该catch这个异常,并采用相应的业务策略来处理,这类异常应该是Checked Exception;但如果一个方法仅仅是检测到数据库连不上,Caller对数据库异常也没啥办法,catch它也没有任何意义 –>既然没有意义,就不要浪费代码去处理它 –>所以干脆就把这类异常做成Runtime Exception. 但是另有一个要点就是: 对Runtime Exception一定要在方法的签名处进行注释,这样Caller才知道此方法可能会抛RuntimeException,这样它就可以自己决定是否要catch这类Exception. 因为有时从健壮性或者其他因素考虑,这种catch还是有必要的。

SchemaFactory is not thread-safe

"The SchemaFactory class is not thread-safe. In other words, it is the application’s responsibility to ensure that at most one thread is using a SchemaFactory object at any given moment" See: http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html

单元性能测试?

Rod Johnson 说load测试未必要针对整个Application才能做,我们也可以针对单个业务对象来测。具体该怎么玩我还没玩,这里先记录一些工具名:     1.Grinder     2.Rod自己写的一个无界面的工具包:com.interface21.load

Java Profiling Tools

1. JVM Profiling Option "starting a Sun Java 1.3 JVM with the following arguments will cause it to dump profile output when it quits:   -Xrunhprof:cpu=samples,thread=y" 2. JProbe

对数据库操作进行单元测试

抄自Rod Johnson’s ‘Expert+One-on-One+J2EE+Design+And+Development’ 1.Mock一个DAO接口,测试时不去真正地访问数据库 2.但有时我们的确想测试一下真正的数据库操作,这是可以理解的,但要注意保证测试的可重复性。一般来说,可以这样:    a.应在测试目标操作前插入一些初始数据以供测试    b.测试目标方法    c.测试目标方法之后应清空之前插入的初始数据

测试J2EE程序面临的主要问题[Rod Johnson]

Testing enterprise applications poses many challenges:      Enterprise applications usually depend on resources such as databases, which will need to be considered in any testing strategy.      Testing web applications can be difficult. They don’t expose simple Java interfaces that we can test, and unit testing is complicated by the dependence of web tier components …

测试J2EE程序面临的主要问题[Rod Johnson] Read More »

What ‘Expert One-on-One J2EE Design’ Covers?

What this Book Covers This book covers:       How to make key J2EE architectural choices, such as whether to use EJB and where to implement business logic       J2EE web technologies, and effective use of the Model-View-Controller (MVC) architectural pattern in web applications       How to use EJB 2.0 effectively, including the implications of the …

What ‘Expert One-on-One J2EE Design’ Covers? Read More »

Spring 2.5中 的 UrlFilenameViewController 确实有bug

正如 http://hi.baidu.com/pmzcn/blog/item/aeab6e63c58762600d33fa21.html所说,spring 2.5中,这个类在解析路径时会把context path带进去! 我的解决办法是,自己做一个UrlFilenameViewController类,在这个类中照抄spring 2.0 UrlFilenameViewController的代码(如果下不到源代码,反编译一下也可以,我就是通过反编译搞到代码的)