gpt4 book ai didi

java - 直接从 Spring 配置文件获取值

转载 作者:行者123 更新时间:2023-11-30 01:39:20 25 4
gpt4 key购买 nike

例如,在 Spring 配置中,我有一个像这样的 map -

<util:map id="someMap"
map-class="java.util.HashMap"
key-type="java.lang.String"
value-type="java.lang.String" >
<entry key="A" value="Apple" />
<entry key="B" value="Banana" />
<entry key="C" value="Cherry" />
<entry key="D" value="Durian" />
<entry key="Default" value="None" />

现在,我有一个方法-

private String getFruitName(){
/* result should come from config depending on the
value of this.fruit [a fruit type which can be one
of the first four keys in the map.] */
}

如何直接将条件设置为 spring 配置文件,而不是通过从类中的方法注入(inject) key 来获取值?我可以在配置中使用条件语句并且可以将参数传递给配置吗?

我不想做这样的事情 -

if (someMap.containsKey(fruit)){
fruitName = someMap.get(fruit);
}

最佳答案

不会对 spring 这样做。

Spring将为您的bean提供配置和其他bean,它不打算用于提供这种逻辑。 someMap.get 就是这种工作方式,或者直接将适当的水果注入(inject)到您的 bean 中。

关于java - 直接从 Spring 配置文件获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1448620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com