In my Spring 3.0 app, I have some resources in /WEB-INF/dir
. At runtime I need some of them as an InputStream
(or some other type). How can I retrieve them? Is it possible to inject them as a normal Resource
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You should be able to use :
where
appContext
is your SpringApplicationContext
(specifically, a WebApplicationContext, since you have a webapp)If you do not want to introduce dependency on Spring, follow approach detailed here: Populate Spring Bean's File field via Annotation
All
ApplicationContext
s are, by definition,ResourceLoader
s. This means that they are capable of resolving any resource strings found within their configuration. With this in mind, you can declare your target bean with a setter that accepts anorg.springframework.core.io.Resource
. Then when you configure the target bean, just use a resource path in the value for the property. Spring will attempt to convert theString
value in your configuration into aResource
.Here is an easiest way to do it via annotation:
Here's a full example to retrieve a classpath resource. I use it to grab SQL files that have really complex queries which I don't want to store in Java classes: