Java Tips

Class.getResource(…)

  • 以目前的Class為基準
  • 可使用Absolute Path (“/”代表Classpath Root) or Relative Path (以目前Class為基準)
MyClass.class.getResource("/") → 從Classpath起算
MyClass.class.getResource("config/myfile.txt") → 從目前的Class位置起算

Continue reading Java Tips

Advertisements

Spring Transaction Tips

Concept

  • Caller→Manager(Transaction)→Dao(Inherit Manager Transaction or set a New Transaction)
  • TransactionException is an unchecked exception (extends java.lang.RungtimeException): it will trigger Rollback. Do not catch this exception, otherwise Rollback will not happen automatically.

Continue reading Spring Transaction Tips

Maven Tips

Local Library in pom.xml

舊寫法 (deprecated), systemPath指定jar檔的位置

...
<dependency>
 <groupId>com.oracle</groupId>
 <artifactId>ojdbc14</artifactId>
 <version>10.2.0.5.0</version>
 <scope>system</scope>
 <systemPath>${project.basedir}/repo/com/oracle/ojdbc14/10.2.0.5.0/ojdbc14-10.2.0.5.0.jar</systemPath> 
</dependency>

新寫法 (repo目錄必需按照groupId/artifactId/version建立相對應的目錄存放jar檔)

<repositories>
  <repository>
    <id>local</id>
    <url>file:///${project.basedir}/repo</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.5.0</version>
  </dependency>
  ...

Continue reading Maven Tips