gstackoverflow
gstackoverflow

Reputation: 37034

How can I expand wildcards in Spring properties?

I am developing spring boot application and I want to inject Collections of resources like this:

@Value("${app.users-location}")
Collection<Resource> csvResources;

inside application.properties I wrote following:

app.users-location=/feed/*.csv

But it doesn't work as expected:

/src/main/resources containing a feed folder with 5 CSVs

For that situation I expect to see Collection of 5 elements of type Resource.

How can I achieve it ?

Upvotes: 0

Views: 4134

Answers (3)

gstackoverflow
gstackoverflow

Reputation: 37034

The person with nick m.antkowicz - https://stackoverflow.com/users/4153426/m-antkowicz

provided correct answer but then removed it.

https://stackoverflow.com/a/57373605/2674303

His solution was to use Array instead of list:

@Value("${app.users-location}")
Resource[] resources;

@PostConstruct
public void init(){
    System.out.println(resources);
}

I checked it and it really works!!!

Upvotes: 0

ema
ema

Reputation: 951

Here is discussion regarding your question:

https://github.com/spring-projects/spring-boot/issues/12993

Seems current version doesn't support it

Upvotes: 0

davidxxx
davidxxx

Reputation: 131346

That is valid :

@Value("app.users-location")
String foo; // inject "/feed/*.csv"

And that is also valid (source) :

@Value("classpath*:/feed/*.csv")
Collection<Resource> resources; // inject all resources located in this classpath

But I don't know how to mix them in a single annotation : that is resolve the property and use it with a classpath*: prefix. Maybe that is possible...

Whatever, as alternative I would inject this property app.users-location=/feed/*.csvand I would use @PostConstruct to get resources from that :

import org.springframework.core.io.support.*

@Value("${app.users-location}")
private String usersLocation;
private Collection<Resource> csvResources;

@PostConstruct
public void init(){
    ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();   
    csvResources = 
    Arrays.asList(patternResolver.getResources("classpath*:/" + usersLocation));
}

Upvotes: 2

Related Questions