Search code examples
javaspringdependency-injectionspring-annotationsspring-io

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


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?


Solution

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

    @Value("classpath*:../../cssDir/*.css")
    private Resource[] cssFiles;