Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 获取xml文件的工作路径_Spring Mvc - Fatal编程技术网

Spring mvc 获取xml文件的工作路径

Spring mvc 获取xml文件的工作路径,spring-mvc,Spring Mvc,让我描述一下我的情况如下: 我创建了spring mvc Web应用程序,其结构如下: -Trainingapp -Java Resources -src/test/java -src/test/resources -src/main/java -com.example.controller -com.example.dao -ReadAttributesSer

让我描述一下我的情况如下: 我创建了spring mvc Web应用程序,其结构如下:

-Trainingapp
    -Java Resources
        -src/test/java
        -src/test/resources
        -src/main/java
            -com.example.controller
            -com.example.dao
                -ReadAttributesService.java
            -com.example.ctx
                -AppContext.java
            -com.example.pojos
        -src/main/resources
            -META-INF
                -applicationContext.xml
            settings.xml
<!-- ReadAttributesService bean -->
<bean id="readAttributesService" class="com.example.dao.ReadAttributesService">
   <constructor-arg name="settings" value="settings.xml" />
</bean>
ReadAttributeService的构造函数使用filePath参数将settings.xml解析为XMLBeans。我希望应用程序创建ReadAttributeService的bean,因此我将其放入applicationContext.xml文件中

<!-- ReadAttributesService bean -->
<bean id="readAttributesService" class="com.example.dao.ReadAttributesService">
    <constructor-arg name="filePath" value="src/main/resources/settings.xml" />
</bean>

好的,不管我给applicationContext的filePath值是多少,至少我所做的是不起作用的。最后,我想创建一个新的FileInputStream of settings.xml,但我还没有找到任何方法来实现这一点

简单地说,我想问一下如何在我的spring mvc Web应用程序中创建FileInputStream或类似于绝对路径settings.xml的东西?
高级版谢谢,任何帮助都非常感谢。

如果您只需要
InputStream
(与
FileInputStream
相反),因为
settings.xml
应该在您的类路径上,我会尝试使用。您的XML bean声明应该如下所示:

-Trainingapp
    -Java Resources
        -src/test/java
        -src/test/resources
        -src/main/java
            -com.example.controller
            -com.example.dao
                -ReadAttributesService.java
            -com.example.ctx
                -AppContext.java
            -com.example.pojos
        -src/main/resources
            -META-INF
                -applicationContext.xml
            settings.xml
<!-- ReadAttributesService bean -->
<bean id="readAttributesService" class="com.example.dao.ReadAttributesService">
   <constructor-arg name="settings" value="settings.xml" />
</bean>
Spring将自动将
settings.xml
字符串转换为
Resource
(实际上是
ClassPathResource

从那里,您应该能够对该资源调用
getInputStream()
方法


如果你真的需要一个
文件输入流
,它会变得很棘手。

不,Resource和ClassPathResource就足够了:D谢谢!