在Spring中使用通配符通过注释将文件作为资源列表注入 [英] Inject files as list of resources using wildcard by annotations in Spring

查看:333
本文介绍了在Spring中使用通配符通过注释将文件作为资源列表注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用作春豆的课程。 bean在 applicationContext.xml 中定义,如:

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>

MyClass 看起来像:

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

    // methods etc.
}

所以Spring填充cssFiles字段,扩展名为.css的所有文件在classpath *:../../ cssDir /下。

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.
}

你有吗?任何想法?

推荐答案

如果您愿意使用数组而不是,请尝试以下操作列表

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

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

这篇关于在Spring中使用通配符通过注释将文件作为资源列表注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆