最近的練手項目使用的是 Maven 在管理項目,在使用 Maven 管理項目時,三層的開發時分模塊開發的,parent-dao-service-web,所有的spring+struts + Hibernate的依賴都是加在 parent 上,dao-service-web都是作為子模塊,在模塊之間的關系處理的時候出現了幾個問題:
junit測試包的傳遞依賴失效了
多個配置文件的讀取問題
我在 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配置文件才會這樣命名)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//@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()); } } |
以上這篇如何使用Maven管理項目?Maven管理項目實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。