Inject files as list of resources using wildcard b

2019-02-16 20:52发布

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?

2条回答
女痞
2楼-- · 2019-02-16 20:58

For application.properties (yml) approach:

someFiles=file:/some/path/*.someExtension
查看更多
smile是对你的礼貌
3楼-- · 2019-02-16 21:20

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

@Value("classpath*:../../cssDir/*.css")
private Resource[] cssFiles;
查看更多
登录 后发表回答