Spring mvc 获取xml文件的工作路径
让我描述一下我的情况如下: 我创建了spring mvc Web应用程序,其结构如下: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
-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谢谢!