Inject files as list of resources using wildcard by annotations in Spring
Asked Answered
A

2

8

I have a class which I use as a spring bean. The bean is defined in the applicationContext.xml like:

<bean id="myClass" class="com.example.MyClass">
        <property name="cssFiles" value="classpath*:../../cssDir/*.css"/>
</bean>

And MyClass looks like:

...
import org.springframework.core.io.Resource;
...
public class MyClass {
    private List<Resource> cssFiles;

    // methods etc.
}

So Spring populates the cssFiles field with all the files with .css extension under "classpath*:../../cssDir/" .

Now I am working on moving to full annotation configuration, but I could not manage to do the same thing with annotations. This does NOT work:

...
import org.springframework.core.io.Resource;
...
@Component
public class MyClass {
    @Value("classpath*:../../cssDir/*.css")
    private List<Resource> cssFiles;

    // methods etc.
}

Do you have any idea?

Alary answered 14/7, 2014 at 14:5 Comment(3)
Does it work like this: @Value("classpath*:../../cssDir/*.css") private Resource[] cssFiles;?Hapte
Thank man, it worked, you are a hero! Can you write this as answer so I can mark it as accepted answer?Aronson
Thank you Utku, just what I needed.Lisp
H
17

Try the following, if you're willing to use an array instead of a List:

@Value("classpath*:../../cssDir/*.css")
private Resource[] cssFiles;
Hapte answered 15/7, 2014 at 9:32 Comment(1)
is there a way to construct this resource array in the xml config?Heterogenous
M
1

For application.properties (yml) approach:

someFiles=file:/some/path/*.someExtension
Meteorograph answered 3/11, 2017 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.