Inject a file resource into Spring bean -
What is a good way to inject some file resources in the spring bean? Now I use ServletContext autowire and the bottom is more elegant way to do this in Spring MVC?
@ Controller Public Class Some Controllers {@Autowired Private ServletContext servletContext; @RequestMapping ("/ treatise") public model and visual texts () {servletContext.getResourceAsStream ("/ web-INF / file.txt") in InputStream; // ...}}
Something like this:
@ Controller Public Class SomeController {Personal resource processing; Public zero set resource (resource processing) {this.resource = resource; } @RequestMapping ("/ treatise") in public models and visual texts () {InputStream = resource.getInputStream ()); // ... in.close (); }} In the definition of your bean:
& Lt; / Bean & gt; This will create the /WEB-INF/file.txt path using the ServletContextResource , and inject it into its controller. / P> Note that you can not use component-scanning to detect your controller using this technique, you need a clear Bean Definition.
Comments
Post a Comment