2014年4月21日 星期一

Java Project 讀取 config.properties 設定檔的方式 Part 2

此法是採用 commons-configuration-1.x.jar (apache的OpenSource可以download)所提供的API
還需要commons-lang-2.x.jar

首先一樣把參數設定檔config.properties放在/src下
之後建立一個Class取名為 Constants,內容如下

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class Constants {

private final static String test_property;

static {
PropertiesConfiguration configuration = null;
try {
configuration = new PropertiesConfiguration("config.properties");
} catch(ConfigurationException e) {
e.printStackTrace();
System.out.println(e.getMessages());
}

test_property = configuration.getString("test_property");

}

public String getConfig(){
return test_property;
}

假設您的設定檔(.properties)內容為
test_property = this is a test

如果利用Constants Class來取得您要的設定檔內容呢!?如下

首先建立一個 ConfigTest.java

將剛才建立的Constants import進來(假設在同個package則不必)

接著

Contants cons = new Contants();

String test = cons.getConfig();

則此時 test 已經等於了 "this is a test"


此法的好處在於設定檔所有的內容都可經由Constants Class來取得
不需像上個方法一樣冗長的程式碼一一初始化


缺點是 修改參數後必須重新佈署程式,而且在 config.properties中

前面的 property key 不可含有 . 沒錯 就是點(上個範例可以)
而且等號後面的 key value不可有 , 沒錯就是逗號

以上面 test_property = this is a test 來說不可改為

test.property = this , is a test
這就2個錯誤都有了,其實第一個錯誤是 java String 類別不給使用
而第二個錯誤是 apache 會把逗號判斷成停止點,所以只能抓到 this 後面的字串就抓不到了




Java Project 讀取 config.properties 設定檔的方式

首先
        將設定檔的位置放至在/src下,接著在想要使用設定檔的java class內加上程式碼

private Properties props;

private void loadProperties() throws FileNotFoundException, IOException {
props = new Properties();
try {
InputStream inputStream =      this.getClass().getClassLoader().getResourceAsStream("config.properties");
props.load(inputStream);
//System.out.println("Get Config Properties Successful");
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}

}

private String getConfig(String key) {
return props.getProperty(key);
}

public ConfigTest() throws NamingException, SQLException, FileNotFoundException, IOException {
init();
}

private void init() throws FileNotFoundException, IOException {
loadProperties();
}


假設你的設定檔的內容可能為
test.Property = justTest (注意test後面有個點,其實也可以合在一起,只是特別註明此法可以用不同的符號區隔)

那麼接下來再你需要讀取設定檔的method裡(與剛才程式碼同個class)

String test = getConfig("test.Property");

則此時 test ="justTest"

此法的缺點是當別支class呼叫到些class時,每次都會重新讀取設定檔
但優點也可以說,我不需重新佈署程式,只單純修改程式設定檔參數即可執行新的參數。








2014年4月8日 星期二

初探 Struts 2.x

初探 Struts 2

Structs 2 與 structs 1.x 大大不同

基本上必須認知一點是 Structs 2.x 並不是 Structs 1.x 的延伸
而是xWork(WebWork的核心) 的架構,可謂是集WebWork與Struct 1.x於大成,而且依然採用 MVC
所以從技術上來說 Structs 2.x 算是全新的框架

Structs 2所需採用的 jar檔為,通常需放在 WEB-INF的lib目錄下(版號會因版本不同,但前面大致相同)

struct2-core-2.x.jar
xwork-core-2.x.jar
commons-lang-2.x.jar
commons-fileupload-1.x.jar
freemarker-2.x.jar
ognl-3.x.jar
javassist-3.x.jar
commons-io-2.x.jar
commons-lang3-3.x.jar

通常網路上會提供懶人包,一次把所有的jar包成一個jar檔,但很容易讓整個專案變得太胖,而且
開發者理論上也應該要了解哪些是真的會用的,哪些不會用到

Structs 2.x 與 1.x 基本上配置有點不一樣

在 1.x 的時候,會使用一個叫做 structs-config.xml
而在 2.x 的時候 則是在 src目錄下 建立一個叫 structs.xml
並且在 1.x 時 對於 structs 在web.xml是配置一個 servlet
但在 2.x 時,則是視為一個 filter
如下:
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

假設有使用過 Structs 1.x的人應該知道 structs-config 就是配置 action的地方
而 Structs 2.x 則是在 structs.xml下面有個基本範例

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
    
    <struts>
        <package name="package" extends="struts-default">
            <action name="hello" class="com.company.MyStructs2" method="test">
                <result name="success">index.jsp</result>
            </action>        
        </package>
    </struts>

稍微解釋一下
package name 可隨意取
extends="structs-default" 代表直接使用 struts2.x.jar 中提供的標準設定
action name 就是 可以視為操作行為的名稱
class 就代表 class name,所以是 在 com.company.MyStructs2
method 指的是 在剛才那個 class中有個 method 叫作 test
<result name="success" index.jsp </result>
這是代表 假設 test這個method傳回的結果為 success 則將導向 index.jsp這個頁面

接著如上所示 建立一個 package 名為 com.company,接著 new一個 class名為 MyStructs2
import com.opensymphony.xwork2.ActionSupport;

public class MyStructs2 extends ActionSupport{

    public String test(){
        return "success";
    }

}

如上所示,注意 Struts 2.x 是 extends ActionSupport 而  1.x通常是 Action

接著 請隨意建一個 index.jsp 頁面,當我們要測試 structs 2.x是否運作成功時
則在瀏覽器上輸入 http://serverIP:port/projectName/hello.action

如上所示,測試時 URL 的結尾是採用 .action ,而 structs 1.x 則使用 .do




2014年4月7日 星期一

Invoking 'Spring Project Builder' on '/project' 花費時間太久

最近遇到在Ecslipse使用Spring開發程式時,常常Build WorkSpace時很慢
訊息僅顯示Spring Project Builder on /projectName

看了好幾篇文章,看來這是Spring的小Bug,我不想要撰寫幾行程式,一儲存就要等個
3-5分鐘甚至更久才能測試程式,這裡連續提供幾個我試圖解決的方法

1.在 Eclipse上方Windows-->Preference-->Spring-->Bean Support-->Timeout[sec]改成了600秒

本來還未改此設定之前,default是300秒,但由於專案太大,總是會花超過300秒,改成了600秒之後專案就沒有Error了<忘了Error是啥,意思好像是Build的時候timeout>

2.修改(1)剛開始沒問題,後來出現標題的問題,所以修改設定在
Windows-->Preference-->Spring-->Project Builders
把 Spring Bean Meta Data Builder 不要打勾,如圖
3.如果仍然有問題,因為Spring把所有Libris中所使用的jar檔中 import的class都搜尋了一遍
可以參考設定在 你的專案名字按「右鍵」-->Properties-->Spring-->Beans Support下面的Options把 Enable support <import/> elements in configuration files 試著勾或不勾看看

補充可加速 Spring Tool Initialize

  1. Disable validators: Preferences -> Validation: check Suspend all validators
  2. Disables Spring validation: Preferences -> Spring: uncheck unwanted validation rules
  3. Disable Build Automatically: Preferences -> General -> workspace: uncheck build automatically

參考資料
http://www.bmchild.com/2013/01/speeding-up-springsource-tool-suite-sts.html