![](/img/trans.png)
[英]Unable to locate xml file using ClassPathResource in Spring Java configuration
[英]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,但沒有結果。
理想情況下,您應該制作兩個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.