通过 Annotation 填充 Spring Bean 的 File 字段 [英] Populate Spring Bean's File field via Annotation

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

问题描述

是否可以做这样的事情:

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 自定义注释并相应地初始化 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;

您也可以通过这种方式使用 classpath: 样式的 URI.

You can use classpath: style URIs this way too.

那么如果你需要获取一个文件,使用template.getFile()

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

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

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