有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从引用bean的映射中获取键值

我试图从引用bean中获取映射键值

我有两门课。一个包含两个映射,另一个需要从这个类中读取键值并设置自己的字段

@Data
public class ClassOne{

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

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

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>

我的意图是让bean ClassOne通过键获取映射和映射值

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

编辑:

对于这个应用程序上下文,我得到

   <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>

以下错误消息:

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}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx['test']' in value "#{ClassOne.${xxx['test']} == 'test' ? 'test1 ' : test2}"


共 (1) 个答案

  1. # 1 楼答案

    我认为问题在于在访问映射时使用${},因为在访问类atribute时不需要使用${}。试试#{ClassOne.xxx['key'] == 'myString' ? 'otherString' : false}

    除此之外,在应用程序上下文中定义了三元运算符。xml返回一个布尔值,以防键不在映射中,并尝试将其分配给字符串属性