【问题标题】:java.lang.IllegalArgumentException: Invalid boolean @Value("${com.test.isTestEnable")java.lang.IllegalArgumentException: 无效的布尔值@Value("${com.test.isTestEnable")
【发布时间】:2020-04-22 18:30:31
【问题描述】:

我得到 **原因:org.springframework.beans.TypeMismatchException:无法将“java.lang.String”类型的值转换为所需的“java.lang.Boolean”类型;嵌套异常是 java.lang.IllegalArgumentException: **

我相信这是由于缺少弹簧占位符配置而发生的。但是我在应用程序上下文中初始化了 bean 并且仍然收到此错误。谁能帮帮我??

    @Value("${com.test.isTestEnable")
    public Boolean isTestEnable;

弹簧属性类

public class SpringPropertiesUtil extends PropertyPlaceholderConfigurer {

    private static HashMap<String, String> systemPropertiesMap;

    private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;

    public static String getProperty(final String name) {
        return systemPropertiesMap.get(name);
    }

    @Override
    protected void processProperties(final ConfigurableListableBeanFactory beanFactory, final Properties props) throws BeansException {
        super.processProperties(beanFactory, props);
        systemPropertiesMap = new HashMap<String, String>();
        for (final Object key : props.keySet()) {
            final String keyStr = key.toString();
            final String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
            systemPropertiesMap.put(keyStr, valueStr);
        }
    }

    @Override
    public void setSystemPropertiesMode(final int systemPropertiesMode) {
        super.setSystemPropertiesMode(systemPropertiesMode);
        springSystemPropertiesMode = systemPropertiesMode;
    }

我的应用程序上下文文件

  <bean id="placeholderConfig" class="com.test.SpringPropertiesUtil">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
            <list>
                <value>file:${config.path}/application.properties</value>
                <value>file:${config.path}/log.properties</value>
            </list>
        </property>
    </bean>

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    您的真正问题只是 @Value("${com.test.isTestEnable") 的拼写错误 - 您在表达式末尾缺少一个右大括号 }

    这应该可以正常工作:

        @Value("${com.test.isTestEnable}")
        public Boolean isTestEnable;
    

    【讨论】:

      【解决方案2】:

      尝试更改以下行

      @Value("${com.test.isTestEnable")
      

      @Value("#{new Boolean('${com.test.isTestEnable}')}")
      

      @Value("#{T(Boolean).parseBoolean('${com.test.isTestEnable}')}")
      

      【讨论】:

      • 感谢 "@Value("#{new Boolean('${com.test.isTestEnable}')}")" 这就是我迄今为止一直在使用的方法。尽管我有其他项目在没有上述配置的新布尔值的情况下也能正常工作。可能是我缺少其他一些配置或初始化吗?
      • 我不这么认为......这是关于将属性值转换为布尔值。看看那里,可能是其他项目中的字段类型是字符串。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 2022-01-14
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多