14 4月 2007

行旅、旅行

今天看到商業周刊裡蔣勳談宋代極簡美學,有許多的角度不是單純的欣賞作品,而是提及了時代的現象,普世的想法,作者個人的心境,較之其他的文章來說,實在有趣不少。 其中提到一段“宋朝人知道,不能自大到認為可以征服宇宙,我們只是宇宙的過客,所以,用「行旅」,而不是「旅行」。人要尊敬自然,要留下謙卑“。 行旅則視我為客,旅行則視我為主,簡單的序順不同,卻有截然不同的感觸,再來看這幾幅畫,感覺就完全不同了。 看完文章,欣賞完畫,在心頭重覆的,竟只有「行旅、旅行」…

13 3月 2007

Dependency checking mechanism

Dependency checking mechanism 在使用Spring的dependency injection功能時,有不少人會希望使用constructor injection,主要是因為這樣同時可以確保所有的dependency都一定會被設定到,但我個人比較偏好使用method injection,一方面這樣比較好使用mock object來做測試,一方面我也不喜歡看到一長串難以分別順序的constructor,而且如果要新增一個subclass,那同時也要extends一個至少一樣長的Constructor... Spring forum最近發起了一個新的投票 How do you ensure required dependencies are set?,看看大家最常用的Dependency檢核機制是什麼,目前看來還是以Annotation @Required, implements InitializingBean後以assert檢查及Constructor injection三者略多,當然也有不少票是投給完全不檢查的... 因為我習慣使用method injection,所以在允許的環境下(一定要用JDK5),我也喜歡用@Required來檢查,如果環境不允許,我就會變成完全不檢查... 以spring提供的例子來看@Required的用法 在要檢查Dependency的setter method加上@Required
public class SimpleMovieLister {
 // the SimpleMovieLister has a dependency on the MovieFinder
 private MovieFinder movieFinder;
 // a setter method so that the Spring container can 'inject' a MovieFinder
 @Required
 public void setMovieFinder(MovieFinder movieFinder) {
  this.movieFinder = movieFinder;
 }
}
然後還需要在xml中加入
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
做為執行檢查@Required之用 如果原本開發系統已經有類似的Annotation,或是不想之後一定要使用Spring,可以使用自定的Annotation
package org.elliot.jdk;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mandatory {

}
在xml只要加入像下列config即可使用自訂的Annotation達成相同的效果
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
 <property name="requiredAnnotationType" value="org.elliot.jdk.Mandatory"/>
</bean>

07 3月 2007

記得

是不是有這樣的一首歌,這樣的一些人, 總在人聲漸歇時在思緒裡緩緩地浮起? 我還記得 --梁靜茹 絲路 十年後的今天遇見你 年少輕狂已遠去 成熟穩重也保持距離 沉默裡千言萬語 時光回到那年夏天 公車站前你笑容滿面 拍拍我的頭 說你好嗎? 一句問候填滿青春 別人的話都聽不見 歲月凝結在你的視線 我還記得那年傾盆大雨 狼狽奔跑穿越幾條街 握緊的雙手 為愛的不顧一切 我還記得那年你的聲音 耳邊迴盪那一句誓言 你吻我的臉 都是我心中 收藏一生的快樂 漸漸我們都有新朋友 多久不再並肩走 忙忙碌碌看人生匆匆 忘記了年輕的夢 好想回到那年夏天 教室門前你笑容滿面 拍拍我的頭 說妳別哭 考壞一次不是末日 未來還有很長的路 我們要一起去看世界 我還記得那年晴空萬里 那一道飛機雲的弧線 蜿蜒著思念 寫下故事的總結 我還記得那年你的年輕 刻在從前最美的時間 在我生命裡 你不曾告別 不曾走遠 與你重逢前一個夜晚 往事在夢中上演 終要去體驗 真實人生的殘缺 我還記得那年晴空萬里 那一道飛機雲的弧線 蜿蜒著思念 寫下故事的總結 我還記得那年你的年輕 刻在從前最美的時間 在我生命裡 你不曾告別 不曾走遠

True Colors

True Colors - Cyndi Lauper You with the sad eyes don't be discouraged oh I realize it's hard to take courage in a world full of people you can lose sight of it all and the darkness inside you can make you fell so small But I see your true colors shining through I see your true colors and that's why I love you so don't be afraid to let them show your true colors true colors are beautiful like a rainbow Show me a smile then don't be unhappy, can't remember when I last saw you laughing if this world makes you crazy and you've taken all you can bear you call me up because you know I'll be there And I'll see your true colors shining through I see your true colors and that's why I love you so don't be afraid to let them show your true colors true colors are beautiful like a rainbow 這大概是我iTunes裡撥放次數最高的歌曲,間歇而有力的簡單節奏,略帶沙啞而獨特的嘶喊,鼓舞著總是略帶遲疑的我----做自己。

05 3月 2007

Struts2 Note -- (3)Action Method Invocation

Action method invocation 在Struts中使用DispatchAction及設定parameter來操做同一個Action的不同Method,可以減去不少的Action數量. 在Struts2中,可以使用wildcard來達成.如下列這個Action. MyAction.java
public class MyAction extends ActionSupport {
 public String input() {
  return "patha";
 }
 
 public String output() {
  return "pathb";
 }
}
然後在struts.xml中以"*"來代表特定組合的文字,再以"{num}"取用. struts.xml
<package name="myaction" namespace="/struts2" extends="struts-default">
 <action name="myaction_*" method="{1}" class="MyAction">
  <result name="patha">
   /jsp/inputResult.jsp
  </result>
  <result name="pathb">
   /jsp/outputResult.jsp
  </result>
 </action>
</package>
這樣的話執行 http://localhost:8080/mystruts/struts2/myaction_input.action 會呼叫MyAction.input()->patha 而下列的URL http://localhost:8080/mystruts/struts2/myaction_output.action 會呼叫MyAction.output()->pathb 看起來便利性上兩者相差不遠,不過wildcard的用途還不止如此....