2015-02-08 29 views
0

我在Spring中得到了以下情況。在Spring中查找spring數據庫XML文件的位置?

src/main/java和一些子文件夾中的spring-database.xml文件(包含hibernate beans,sessionfactory,dao等)。 我一直無法使用保存在MySQL表中的日誌記錄用戶。

因此,我在WEB-INF下移動了spring-database.xml,在這裏工作。 問題是,現在我不知道如何將它從Java代碼調用:

ApplicationContext appContext = new ClassPathXmlApplicationContext([PATH I DON'T KNOW]) 

,因爲這不工作,我需要從該文件中一些豆子,這是不是現在發現。 我也嘗試過FileSystemXmlApplicationContext而不是ClassPathXmlApplicationContext而沒有任何結果。

  1. 什麼是找到這個XML文件的最佳解決方案?在src/main/java下還是在WEB-INF下?
  2. 如何正確調用它的bean而不會搞亂相對/絕對路徑?

回答

0

理想情況下,你應該讓兩個XML文件

1. mvc-config.xml or your spring-database.xml (with hibernate, sessionfactory etc) 
2.application-config.xml(this should contain all the bean relates info that you should need in your project.) 

請在classpath中你的WEB-INF /文件夾和apllication-config.xml中的MVC-config.xml中。

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
    classpath:spring/application-config.xml, 
    /WEB-INF/conf/mvc-config.xml 
    </param-value> 
</context-param> 

這是你如何在web.xml加載兩個 您將能夠從您要在上面的代碼中獲得的application.xml。

Look at this example.