简体   繁体   English

从参考bean的映射中获取键值

[英]Get key value from map from reference bean

Im trying to get map key value but from reference bean. 我试图从引用bean获取地图键值。

I have two classes. 我有两节课。 One contains two Maps other one need to read key value from this class an set own field. 一个包含两个Map,另一个需要从此类读取键值并设置一个自己的字段。

@Data
public class ClassOne{

    private Map<String, Object> xxx;
    private Map<String, Object> yyy;

}
@Data
public class ClassTwo{
    private String zzz;
}

xml application context xml应用程序上下文

<bean id="ClassTwo" class="...">
<property name="zzz" value="#{ClassOne.${xxx['key']} == 'myString' ? 'otherString' : false}"></property>
</bean>

<bean id="ClassOne" class="...">
<property name="xxx" value=""></property>
</bean>

my intention is to refer to bean ClassOne get a Map and get Map value by key 我的意图是引用bean ClassOne获取一个Map并通过键获取Map值

{ClassOne.${xxx['key']} == 'myString' ? {ClassOne。$ {xxx ['key']}} =='myString'吗? 'otherString' : false} 'otherString':false}

edit: 编辑:

for this application context Im getting 对于此应用程序上下文我越来越

   <bean id="ClassTwo" class="com.interns.pittaci.model.label.ClassTwo">
        <!--- for property name zzz from ClassTwo set value based on map called xxx with key 'test' inside ClassOne -->
        <property name="zzz" value="#{ClassOne.${xxx['test']} == 'test' ? 'test1 ' : test2}"></property>
    </bean>

following error message: 以下错误消息:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'ClassTwo' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'xxx['test']' in value "#{ClassOne.${xxx['test']} == 'test' ? 'test1 ' : test2}"; 线程“主”中的异常org.springframework.beans.factory.BeanDefinitionStoreException:在类路径资源[applicationContext.xml]中定义的名称为“ ClassTwo”的bean定义无效:无法在值“中解析占位符'xxx ['test']'” #{ClassOne。$ {xxx ['test']}} =='test'?'test1':test2}“; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx['test']' in value "#{ClassOne.${xxx['test']} == 'test' ? 'test1 ' : test2}" 嵌套异常是java.lang.IllegalArgumentException:无法解析值“#{ClassOne。$ {xxx ['test']}} =='test'?'test1':test2}”中的占位符'xxx ['test']'

I think the problem is using ${} when accessing to the map because it is not necessary to use ${} when accesing a class atribute. 我认为问题在于访问地图时使用了$ {},因为访问类属性时不必使用$ {}。 Try #{ClassOne.xxx['key'] == 'myString' ? 'otherString' : false} 尝试#{ClassOne.xxx['key'] == 'myString' ? 'otherString' : false} #{ClassOne.xxx['key'] == 'myString' ? 'otherString' : false}

In addition to this, the ternary operator defined in application-context.xml is returning a boolean value in case that the key is not present in the map and trying to assign it to a String attribute 除此之外,在映射中不存在键的情况下,application-context.xml中定义的三元运算符将返回布尔值,并尝试将其分配给String属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM