让我描述一下我的情况如下: 我创建了我的spring-mvc webapplication,其结构如下:
-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的构造函数使用filePath参数从settings.xml解析为XMLBeans。我希望应用程序创建一个ReadAttributesService 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的值是什么,都不会起作用,至少我已经完成了。最后,我想创建一个新的settings.xml的FileInputStream,但我还没有找到任何方法来实现这一点。
所以说这很简单,我想问一下如何在spring-mvc webapplication中创建一个FileInputStream或类似于settings.xml的AbsolutePath? 先谢谢你,任何帮助都非常适合。
答案 0 :(得分:0)
如果您只需要InputStream
(而不是FileInputStream
),由于settings.xml
应该在您的类路径上,我会尝试使用Spring Resources。您的XML bean声明应如下所示:
<!-- ReadAttributesService bean -->
<bean id="readAttributesService" class="com.example.dao.ReadAttributesService">
<constructor-arg name="settings" value="settings.xml" />
</bean>
使用ReadAttributesService
的构造函数arg:
public class ReadAttributesService {
public ReadAttributesService(org.springframework.core.io.Resource settings){
...
}
...
}
Spring会自动将settings.xml
字符串转换为Resource
(实际上是ClassPathResource
)。
从那里你应该能够在该资源上调用getInputStream()
方法。
如果你确实需要FileInputStream
,那就太棘手了。