在Spring中查找spring数据库XML文件的位置?

问题描述:

我在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而不会搞乱相对/绝对路径?

理想情况下,你应该让两个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.