使用 Maven 管理項目,使用maven管理項目
最近的練手項目使用的是 Maven 在管理項目,在使用 Maven 管理項目時,三層的開發時分模組開發的,parent-dao-service-web,所有的spring+struts + Hibernate的依賴都是加在 parent 上,dao-service-web都是作為子模組,在模組之間的關係處理的時候出現了幾個問題:
我在 parent 工程沒有添加 Junit 的依賴,在編寫 dao 模組是添加了 Junit 的 jar 包,理所當然的在 scope 中寫了 test 範圍,但是在 service 模組中進行 Junit 測試時,顯示沒有依賴上 Junit 包,那是為什麼呢?百度了才想通,原來是 service 依賴的 dao 模組的 install 之後的 jar 包,當 dao 模組 install 時,scope 為 test 的 Junit包當然沒有被發布出來,service中也就不能傳遞依賴到 Junit了,這樣的解決辦法只能在 service 中添加 Junit 包的依賴。
因為採取的是模組式的開發,spring的設定檔就被分布在各個模組中了,在測試專案時需要讀取多個模組中的 spring 設定檔時,使用到了之前沒有使用到的一個註解:
@ContextConfiguration(locations={"classpath*:applicationContext-*.xml"}) 這個註解中的*號萬用字元表示會載入本模組和依賴的jar包中的類路徑下的applicationContext-開頭的設定檔(只有spring設定檔才會這樣命名)
//@ContextConfiguration(locations={"classpath*:applicationContext-*.xml"})@ContextConfiguration(locations={"classpath:applicationContext-dao.xml","classpath:applicationContext-service.xml"})@RunWith(SpringJUnit4ClassRunner.class)public class CustomerServiceImplTest { @Autowired private CustomerService customerService; @Test public void test() { Customer customer = customerService.findById(1L); System.out.println("********************"+customer.getCustName()); }}