顯示具有 程式語言 標籤的文章。 顯示所有文章
顯示具有 程式語言 標籤的文章。 顯示所有文章

2009年4月16日 星期四

Spring Transaction

下面是在Spring 2 + Hibernate 3 + MySQL 5架構中使用無Transaction的設定檔(XML)



<bean id="individualMgnt" class="component.IndividualMgnt">
<property name="individualDao" ref="individualDao"/>
<property name="individualPartyDao" ref="individualPartyDao"/>
<property name="locationDao" ref="locationDao"/>
<property name="partyDao" ref="partyDao"/>
<property name="roleDao" ref="roleDao"/>
</bean>



下面是在Spring 2 + Hibernate 3 + MySQL 5架構中使用Spring Transaction的設定檔(XML)


<bean id="individualMgnt"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<bean class="component.IndividualMgnt">
<property name="individualDao" ref="individualDao"/>
<property name="individualPartyDao" ref="individualPartyDao"/>
<property name="locationDao" ref="locationDao"/>
<property name="partyDao" ref="partyDao"/>
<property name="roleDao" ref="roleDao"/>
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, timeout_30, readOnly
</prop>
<prop key="add*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, timeout_-1, -MyException
</prop>
<prop key="remove*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, timeout_-1, -MyException
</prop>
</props>
</property>
</bean>

Read more...

2009年4月15日 星期三

好用的HTML轉碼小工具

經常在部落格中放置程式碼或是XML設定檔的人一定會發現,有些特殊字元要先行轉換成特殊編碼,瀏覽器才能正確顯示,這時就要感謝這CENTRICLE.COM網站,它提供了一個好用的小工具協助我們編寫HTML文件。

Read more...

2009年2月21日 星期六

在Spring 2 + Hibernate 3架構中以datasource方式使用Proxool 0.9 RC3的Connection Pool

下面是在Spring 2 + Hibernate 3 + MySQL 5架構中以datasource方式使用Proxool 0.9 RC3的Connection Pool的設定檔(XML)



<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias"><value>mysql</value></property>
<property name="driver"><value>com.mysql.jdbc.Driver</value></property>
<property name="driverUrl"><value>jdbc:mysql://localhost:3306/extreme?user=root&amp;password=123456</value></property>
<property name="user"><value>root</value></property>
<property name="password"><value>tsaijs</value></property>
<property name="houseKeepingSleepTime"><value>5000</value></property>
<property name="houseKeepingTestSql"><value>SELECT CURRENT_DATE</value></property>
<property name="maximumConnectionCount"><value>20</value></property>
<property name="minimumConnectionCount"><value>1</value></property>
<property name="simultaneousBuildThrottle"><value>30</value></property>
<property name="trace"><value>true</value></property>
<property name="verbose"><value>true</value></property>
</bean>

Read more...

2008年11月25日 星期二

Unexpected Multithreading on Spring2 IoC Container

目前小弟我在執行的工作案是使用Spring2 + Hibernate3 + Flex3 + BPM + Multithreading技術的環境,有一天客戶提出的Scenario是需要用到Multithreading的,因為沒有注意到Multithreading這個條件,所以,就跟以往一樣開始撰寫Java程式和設定Spring XML configuration,在整個開發過程中都有進行單元測試,而且都很順利。但是,最後進行整合測試時,竟然發現有些模組會出現前一次執行的結果,而不會產生現在所要的結果,這下可頭大了。只好從頭開始將每個可能發生錯誤的模組進行測試,結果跟開發時一樣都沒有問題,最後只好使用Debug模式一行一行Trace程式碼,就在此時發現透過Spring的IoC注入模組並不會產生新的結果,而是直接回覆舊有的資料,因此就發現到可能是這個模組被設定成Singleton模式,因此,小弟我就立刻去翻閱Spring 2的文件,在Spring BeanFactory的XML Configuration章節中有提到,所有Spring beans的內定值都是Singletons,所以,當你想要定義一個Prototype Bean,就要在 XML 的設定檔中註明 scope="prototype",範例如下:



<bean id="studentDao" class="com.spring.service.StudentDaoImpl" scope="prototype">


Reference: http://static.springframework.org/spring/docs/2.0.x/reference/index.html

Read more...

2008年9月24日 星期三

Java語言中的 interface 和 abstract class 的比較

如果你有在開發 Java 程式語言的話,應該常常會遇到什麼狀況應該使用 abstract class ,什麼狀況應該使用 interface , 還是兩者都一起用呢? Interface 和 abstract class 表面上好像提供幾乎相同的能力,那如何決定使用哪個呢?
什麼時機使用 Interfaces
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.
什麼時機使用Abstract classes
An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation.
The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.
什麼時機使用兩者一起使用
You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.
比較表



Interfaces vs Abstract Classes
feature
interface
abstract class
multiple inheritanceA class may implement several interfaces.A class may extend only one abstract class.
default implementationAn interface cannot provide any code at all, much less default code.An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
constantsStatic final constants only, can use them without qualification in classes that implement the interface. On the other paw, these unqualified names pollute the namespace. You can use them and it is not obvious where they are coming from since the qualification is optional.Both instance and static constants are possible. Both static and instance intialiser code are also possible to compute the constants.
third party convenienceAn interface implementation may be added to any existing third party class.A third party class must be rewritten to extend only from the abstract class.
is-a vs -able or can-doInterfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.An abstract class defines the core identity of its descendants. If you defined a Dog abstract class then Dalmatian descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is. In a Java context, users should typically implement the Runnable interface rather than extending Thread, because they’re not really interested in providing some new Thread functionality, they normally just want some code to have the capability of running independently. They want to create something that can be run in a thread, not a new kind of thread.The similar is-a vs has-a debate comes up when you decide to inherit or delegate.
plug-inYou can write a new replacement module for an interface that contains not one stick of code in common with the existing implementations. When you implement the interface, you start from scratch without any default implementation. You have to obtain your tools from other classes; nothing comes with the interface other than a few constants. This gives you freedom to implement a radically different internal design.You must use the abstract class as-is for the code base, with all its attendant baggage, good or bad. The abstract class author has imposed structure on you. Depending on the cleverness of the author of the abstract class, this may be good or bad.
homogeneityIf all the various implementations share is the method signatures, then an interface works best.If the various implementations are all of a kind and share a common status and behaviour, usually an abstract class works best. Another issue that’s important is what I call "heterogeneous vs. homogeneous." If implementors/subclasses are homogeneous, tend towards an abstract base class. If they are heterogeneous, use an interface. (Now all I have to do is come up with a good definition of hetero/homo-geneous in this context.) If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they share is a set of method signatures, then tend towards an interface.
maintenanceIf your client code talks only in terms of an interface, you can easily change the concrete implementation behind it, using a factory method.Just like an interface, if your client code talks only in terms of an abstract class, you can easily change the concrete implementation behind it, using a factory method.
speedSlow, requires extra indirection to find the corresponding method in the actual class. Modern JVMs are discovering ways to reduce this speed penalty.Fast
tersenessThe constant declarations in an interface are all presumed public static final, so you may leave that part out. You can’t call any methods to compute the initial values of your constants. You need not declare individual methods of an interface abstract. They are all presumed so.You can put shared code into an abstract class, where you cannot into an interface. If interfaces want to share code, you will have to write other bubblegum to arrange that. You may use methods to compute the initial values of your constants and variables, both instance and static. You must declare all the individual methods of an abstract class abstract.
adding functionalityIf you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

Reference:
interface vs abstract class : Java Glossary

Read more...

2008年6月13日 星期五

Java Threads Programming - MyDaemon.java

Java程式語言中提供了二種方法來撰寫Thread程式:
1. extents java.lang.Thread class
2. implements java.lang.Runnable interface
由於Java程式語言採用類別單一繼承方式,如果我們的類別已經繼承某個類別,那麼就不能再繼承Thread類別,也就是說,繼承了Thread類別,就不能再繼承其它類別。因此,還可以利用第二種方式來實現Thread功能,此方式需實作在Runnable介面中定義的run()方法,然後在new一個Thread物件時,傳入一個實作Runnable介面的物件作為引數,Thread物件會利用Runnable物件的run()方法,進而執行當中所定義的程式碼。所以,基本上是建議以實作Runnable的方式讓物件具有執行緒功能,也保留日後修改的彈性,而且利用Runnable介面來撰寫執行緒,程式較有一致性,當其他程式要用到時,可以有共同介面的標準,也比較符合OO的精神。
使用Runnable方式的程式碼如下:


Daemon.java
public abstract class Daemon implements Runnable {
protected Object executeObject = null;
Thread thread = null;

public Daemon(Object executeObject) {
this.executeObject = executeObject;
thread = new Thread(this);
}

public void run(){
doRun();
}

public abstract void doRun();

public void terminate() {
doTerminate();
}

public void doTerminate(){
}

public void start(){
thread.start();
}

public void stop() {
thread.interrupt();
}
}


MyDaemon.java
public class MyDaemon extends Daemon {

public MyDaemon(Object executeObject) {
super(executeObject);
// TODO Auto-generated constructor stub
}

@Override
public void doRun() {
// TODO Auto-generated method stub
SomeJob object = (SomeJob) this.executeObject;
object.run();
}
}


SomeJob.java
public class SsomeJobThread {
private int var1;

public void run() {
// do something ...
}

public int getVar1() {
return var1;
}

public void setVar1(int Var1) {
this.Var1 = var1;
}
}

呼叫方法如下:

SomeJob job = new SomeJob();
job.setVar1(123);
MyDaemon daemon = new MyDaemon(job);
daemon.start();

這個Daemon物件會自動幫我產生一個thread去執行我定義的工作,執行完後就自動消失了,不錯吧!

Read more...

  © Blogger template Spain by Ourblogtemplates.com 2008

Back to TOP