通过Annotation填充Spring Bean的File字段

lis*_*sak 11 spring annotations file

有可能做这样的事情:

<property name="template" value="file:/some/resource/path/myTemplate.txt" />
Run Code Online (Sandbox Code Playgroud)

通过注释而不创建自定义注释并通过反射填充该字段?它不能用@Value完成,因为它只适用于字符串......

如果没有,那么处理Spring bean自定义注释并相应地初始化bean的最佳方法是什么?

sou*_*ica 26

使用资源作为字段.

@Value("file:/some/resource/path/myTemplate.txt")
Resource template;
Run Code Online (Sandbox Code Playgroud)

您也可以这样使用classpath:样式URI.

然后,如果您需要获取文件,请使用 template.getFile()