在採用 Struts2 + Spring + Hibernate (SSH2) 且使用註解開發的專案中,啟動時拋出找不到 hibernate.cfg.xml 與 applicationContext.xml 的異常。即使 web.xml 已配置 ContextLoaderListener 指向 classpath:/config/spring/*.xml,問題依舊存在。
hibernate.cfg.xml 應併入 applicationContext.xml 中,由 LocalSessionFactoryBean 管理。src/config/spring 目錄下存在 applicationContext.xml。hibernate.cfg.xml 中的資料源、映射等配置遷移至 applicationContext.xml 的 sessionFactory Bean 定義中。hibernate.cfg.xml,避免雙重配置衝突。web.xml 中 contextConfigLocation 路徑是否與實際檔案位置一致。採用純註解配置時,建議完全捨棄 .cfg.xml,改用 Spring 的 @Configuration + @Bean 方式定義 SessionFactory,實現零 XML 配置。