Pages

Tuesday, June 26, 2012

How to use Spring Resources in WAR Example

Problem: Assume you have a some resources in your WAR file, that you want to use in run time, for example accessing XML files and printing it's content, or reading some meta-data and using it.

Solution: Spring provided an useful Resource abstraction, that can be used. To obtain a reference to a service that will allow access to resource, you can use:

@Autowired
private ResourcePatternResolver resourceLoader = null;


Spring will inject a bean that implements this interface, in practice it would actually be a reference for ApplicationContext (ApplicationContext must implement ResourcePatternResolver interface)

and then you can get an array of resources by using:

Resource[] resources = resourceLoader.getResources("resources/my-xml-files/**");

After that, you can iterate over the resources and easily perform

resource.getInputStream()

to read the contents.

Good luck

1 comment:

yachiracey said...

Pragmatic Play launches mobile live dealer slot games - JDH
Pragmatic Play, a leading content provider to 삼척 출장마사지 the 안산 출장마사지 gaming industry, today launched a 광양 출장샵 live dealer version of 천안 출장안마 its 군포 출장안마 popular slot games Pragmatic Play.