(填坑)關於spring 依賴注入的javaConfig和xml配置混合使用在junit測試中的測試失敗。

首先我們可能會有這樣的需求:依賴注入在javaConfig實現顯示配置,而AOP在XML中配置,這就需要javaConfig和xml配置混合使用,就像這樣:
@Configuration
@ComponentScan
@ImportResource("classpath*: soundsystem/BlankDisc.xml")

public class TrackCounterConfig {



}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCounterConfig.class)
public class TrackCounterTest {}

在javaConfig中用@ImportResource把xml導入進去,然後junit測試時報錯,提示錯誤大概是xml中的內容被忽略。

但是如果把配置只寫在Javaconfig或者Xml中,junit測試就能正確運行


後來我嘗試:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCounterConfig.class,locations = "classpath*: soundsystem/BlankDisc.xml")
public class TrackCounterTest {}

在junit @ContextConfiguration中雙導入,也同樣報錯,提示錯誤是classes和locations不能同時使用,這時我猜測應該是junit的作者偷懶!!!假如你在@ContextConfiguration中寫的是Classes,junit掃描的時候就只會掃描class文件,而忽略所有的xml文件,location時也是同理。(我用的是IDEA+junit4情況是如此,其他用Ecplise或者junit5就不知道是不是這樣了)


還有在

locations = "classpath*: soundsystem/BlankDisc.xml

中classpath後面儘量寫個*,不然會報錯。


在寫Spring AOP xml配置時,要導入幾個aspectj的包,不然也會報錯,詳情請看

點擊打開鏈接



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章