栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring:如何用注解替换Constructor-arg?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring:如何用注解替换Constructor-arg?

我认为您所追求的是:

@Componentpublic class MyBean {    private String xmlFile;    private String xsdFile;    @Autowired    public MyBean(@Value("$MYDIR/myfile.xml") final String xmlFile, @Value("$MYDIR/myfile.xsd") final String xsdFile) {        this.xmlFile = xmlFile;        this.xsdFile = xsdFile;    }    //methods}

您还可能希望通过系统属性来配置这些文件。您可以使用

@Value
注释通过
PropertyPlaceholderConfigurer
${}
语法读取系统属性。

为此,您可以

String
@Value
注释中使用不同的值:

@Value("${my.xml.file.property}")@Value("${my.xsd.file.property}")

但您还将在系统属性中需要以下属性:

my.xml.file.property=$MYDIR/myfile.xmlmy.xsd.file.property=$MYDIR/myfile.xsd


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508607.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号