Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 422
Utku Özdemir Avatar asked Jul 14 '14 14:07

Utku Özdemir


People also ask

What are @resource and @inject annotations in Spring Framework?

1. Overview In this Spring Framework tutorial, we'll demonstrate how to use annotations related to dependency injection, namely the @Resource, @Inject, and @Autowired annotations. These annotations provide classes with a declarative way to resolve dependencies: As opposed to instantiating them directly (the imperative way):

What happens if @resource annotation is not using name attribute?

If @Resource annotation is not using name attribute, Spring accesses other beans for matching them by type. 6. The @Resource can be used on a a. Field b. Method 7. The @Resource can be used by b. type attribute. 1. In Spring, the JSR-250 @Resource and Spring @Autowired both annotations are used to solve dependency injection.

What is the difference between @resource and @AutoWired annotations?

In Spring, the JSR-250 @Resource and Spring @Autowired both annotations are used to solve dependency injection. The @Resource is supported only for fields and bean property setter methods with a single argument whereas @Autowired applies to fields, constructors, and multi-argument methods. 2.

What are Java annotations in spring?

This Spring tutorial helps you understand how to use Java annotations to configure dependency injection for classes in an application. Besides using XML for dependency injection configuration, Spring also allows programmers to embed some special annotations into Java classes to do the same thing.


2 Answers

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

@Value("classpath*:../../cssDir/*.css")
private Resource[] cssFiles;
like image 195
Andrei Stefan Avatar answered Sep 17 '22 14:09

Andrei Stefan


For application.properties (yml) approach:

someFiles=file:/some/path/*.someExtension
like image 35
user2310395 Avatar answered Sep 21 '22 14:09

user2310395