java - ConversionServiceFactoryBean 不起作用

标签 java spring applicationcontext

我正在使用一个特殊的 CacheConfig 对象,该对象包含一个字段(具有标准 getter/setter 方法),accessExpirationValue,其类型为 java.time.Duration 。 (编辑:实际上,该字段的类型为 Long(秒数),但 getter 和 setter 的类型为 Duration。)

我试图通过将此值设置为秒数并使用 ConversionServiceFactoryBean 在 Spring 中连接它,如下所示:

ApplicationContext.xml中的相关bean:

<bean id="conversionService"
    class="org.springframework.context.support.ConversionServiceFactoryBean" >
  <property name="converters">
    <set>
      <bean
          class="com.tjamesboone.example.config.SecondsToDurationConverter"/>
    </set>
  </property>
</bean>

<bean id="cacheConfig" class="com.tjamesboone.example.cache.CacheConfig" >
  <property name="accessExpirationValue" value="0" />
</bean>

SecondsToDurationConverter:

package com.tjamesboone.example.cache;

import java.time.Duration;

import org.springframework.core.convert.converter.Converter;

public class SecondsToDurationConverter implements Converter<String, Duration> {

  @Override
  public Duration convert(String seconds) {
    return Duration.ofSeconds(Long.parseLong(seconds));
  }

}

现在,据我了解,这应该正常工作。当我为 accessExpirationValue 的值传入“0”时,我已经声明了一个处理将字符串转换为持续时间的 conversionService bean,这意味着应该将该值设置为零长度的持续时间。

但这太容易了。确实如此。因为当我测试我的应用程序(使用 SpringJUnit4ClassRunner)时,我收到此错误,就好像我从未注册过转换器:

Bean property 'accessExpirationValue' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

所以我的问题是,我做错了什么?我如何让它按照我想要的方式工作?

作为引用,这是我一直在使用的主要文档: <强> https://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#core-convert-Spring-config

它特别指出,

In a Spring application, you typically configure a ConversionService instance per Spring container (or ApplicationContext). That ConversionService will be picked up by Spring and then used whenever a type conversion needs to be performed by the framework.

编辑: 我可能还应该发布 CacheConfig 的相关部分!

package com.tjamesboone.example.config;

import java.time.Duration;

public class CacheConfig {

  private Long accessExpirationValue;

  public Duration getAccessExpiration() {
    return Duration.ofSeconds(accessExpirationValue.intValue);
  }

  public void setAccessExpiration() {
    this.accessExpirationValue = expirationDuration.getSeconds();
  }
}

最佳答案

Spring 将尝试将 bean 中的属性与类中指定的 getter 和 setter 进行匹配。

您的 getter/setter 当前为 getAccessExpiration(),但应为 getAccessExpirationValue() 以匹配您的 bean 属性 name="accessExpirationValue"。更改其中之一,您就应该拥有它。

关于java - ConversionServiceFactoryBean 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42287928/

相关文章:

java - 以编程方式更改 Android 中的 DatePicker 日历大小

java - Spring Integration RabbitTemplate 默认情况下是否发布到持久队列?

java - Spring Boot应用程序oneToMany关系中的assertTrue异常

java - Maven 将 applicationContext.xml 从 src/main/resources 复制到 target/myproject/WEB-INF

java - Spring 3 在自定义 bean 中接收 servletContext

java - 在 java JDBC 连接错误 ORA-28000 : the account got locked, 但数据库帐户未锁定

java - 我如何使用 arrayAdapter 的 Intent 进入第三个 Activity ?

java - 在 Spring Batch 中使用采用参数的查询

java - 开发模式服务器中的 GWT XML 配置解析错误

java - 从控制台禁用/更改 Spring Boot 的 ApplicationContext 的时间戳