簡體   English   中英

在Spring中哪里可以找到Spring數據庫XML文件?

[英]Where to locate a spring database XML file in Spring?

我在春季遇到以下情況。

src / main / java和一些子文件夾中的spring-database.xml文件(帶有休眠bean,sessionfactory,dao等)。 我無法使其與記錄保存在MySQL表中的用戶一起使用。

因此,我將spring-database.xml移至WEB-INF下,並且在這里起作用了。 問題是現在我不知道如何使用以下代碼從Java代碼中調用它:

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

因為這不起作用,並且我需要該文件中的一些bean,但現在找不到。 我也嘗試了FileSystemXmlApplicationContext而不是ClassPathXmlApplicationContext,但沒有結果。

  1. 找到此xml文件的最佳解決方案是什么? 在src / main / java或WEB-INF下?
  2. 以及如何正確調用其bean而不弄亂相對/絕對路徑?

理想情況下,您應該制作兩個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.)

將mvc-config.xml保留在WEB-INF /文件夾中,將apllication-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。

看這個例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM