通过注释填充的Spring bean的文件字段 [英] Populate Spring Bean's File field via Annotation

查看:173
本文介绍了通过注释填充的Spring bean的文件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能做这样的事情:

Is it possible to do something like this :

<property name="template" value="file:/some/resource/path/myTemplate.txt" />

无需通过创建自定义的注释,并通过反射填充字段注释?它不能与@Value做,因为它只能与弦乐...

via annotations without creating custom annotation and populate the field via reflection ? It cannot be done with @Value, cause it works only with Strings...

如果没有,什么是处理的Spring bean定制注释的最佳方式,并相应地初始化豆?

If not, what is the best way to process Spring bean custom annotations and init the bean accordingly ?

推荐答案

使用资源的领域。

@Value("file:/some/resource/path/myTemplate.txt")
Resource template;

您可以使用类路径:风格的URI这种方式太

You can use classpath: style URIs this way too.

然后,如果你需要得到一个文件,使用 template.getFile()

Then if you need to get a File, use template.getFile()

这篇关于通过注释填充的Spring bean的文件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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