Spring 3.0 inject files as resources

后端 未结 5 1389
星月不相逢
星月不相逢 2020-12-15 15:27

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 retri

5条回答
  •  悲&欢浪女
    2020-12-15 15:55

    All ApplicationContexts are, by definition, ResourceLoaders. 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 an org.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 the String value in your configuration into a Resource.

    public class Target {
      private Resource resource;
      public void setResource(final Resource resource) {
        this.resource = resource;
      }
    }
    
    //configuration
    
      
        
      
    
    

提交回复
热议问题