gpt4 book ai didi

java - 如何在 Spring 配置文件中初始化 boolean 值?

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

事实上我已经寻找过这个问题的答案,但还没有找到解决方案。而且,我是一个十足的Spring菜鸟,继承了这个问题......

当我为项目部署 WAR 文件时,它未能引用从字符串到 boolean 值的转换。详情:

Spring 配置文件包含此(已编辑)元素:

<property name="logTypes">
<map>
<entry key="MESSAGE" value="${xxx.messagelogenabled}" />
<entry key="SUMMARY" value="${xxx.summarylogenabled}" />
<entry key="STATISTICS" value="true" />
<entry key="ERROR" value="true" />
<entry key="WARNING" value="true" />
<entry key="DEBUG" value="${xxx.debuglogenabled}" />
</map>
</property>

“xxx.*logenabled”字符串在其他地方定义为“true”或“false”,但即使我将这些引用替换为“true”,也会失败。

“logTypes”属性的 setter 是:

public void setLogTypes(Map<String, Boolean> logTypesMap)
{ /* I think the problem is with Spring creating the argument
to this method, not the method itself */ }

我收到以下运行时错误,导致部署失败:

Error setting property values;
nested exception is org.springframework.beans.PropertyBatchUpdateException;
nested PropertyAccessExceptions (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'logTypes';
nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

这是我正在运行的 Java 版本:

% java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

最佳答案

您可以指定映射的键类型和值类型

<property name="logTypes">

<map key-type="java.lang.String" value-type="java.lang.Boolean">

<entry key="MESSAGE" value="${xxx.messagelogenabled}" />
<entry key="SUMMARY" value="${xxx.summarylogenabled}" />
<entry key="STATISTICS" value="true" />
<entry key="ERROR" value="true" />
<entry key="WARNING" value="true" />
<entry key="DEBUG" value="${xxx.debuglogenabled}" />
</map>
</property>

关于java - 如何在 Spring 配置文件中初始化 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31416457/

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