【问题标题】:How to setup LocalDateTime in Spring XML configuration如何在 Spring XML 配置中设置 LocalDateTime
【发布时间】:2015-08-12 05:03:48
【问题描述】:

我的问题与以下课程有关:

public class MyClass {
    private LocalDateTime startDate;
}

我正在尝试使用 Spring XML 配置设置此 bean 的 startDate 属性:

<property name="startDate">
   <value>2000-01-01</value>
</property>

我收到一个错误:

Cannot convert value of type [java.lang.String] to required type [java.time.LocalDateTime] for property 'startDate'

是否可以使用 Spring 进行这种转换?我在net 上找到了如何为 Date 对象执行此操作的示例,但是,LocalDateTime 没有采用字符串的构造函数(并且解决方案似乎需要这样的构造函数)。 LocalDateTime 是使用静态方法 LocalDateTime.parse 构造的。

使用注解@DateTimeFormat,如:

public class MyClass {
    @DateTimeFormat(iso=ISO.LOCAL_DATE_TIME)
    private LocalDateTime startDate;
}

不是解决方案,因为 MyClass 必须在 Spring 之外可用。

提前致谢

【问题讨论】:

    标签: java xml spring


    【解决方案1】:

    您可以注册您的转换。像下面的代码一样,下面会将字符串转换为LocalDateTime

    class CustomLocalDateTimeEditor extends PropertyEditorSupport {
    
    private final boolean allowEmpty;
    private final int exactDateLength;
    
    public CustomLocalDateTimeEditor( boolean allowEmpty) {
        this.allowEmpty = allowEmpty;
        this.exactDateLength = -1;
    }
    
    public CustomLocalDateTimeEditor(boolean allowEmpty, int exactDateLength) {
        this.allowEmpty = allowEmpty;
        this.exactDateLength = exactDateLength;
    }
    
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (this.allowEmpty && !StringUtils.hasText(text)) {
            setValue(null);
        }
        else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
            throw new IllegalArgumentException("Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
        }
        else {
            try {
                setValue(LocalDateTime.parse(text));
            }
            catch (DateTimeParseException ex) {
                throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
            }
        }
    }
    
    @Override
    public String getAsText() {
        LocalDateTime value = LocalDateTime.parse(String.valueOf(getValue()));
        return (value != null ? value.toString() : "");
    }
    

    }

    【讨论】:

      【解决方案2】:

      这是我根据 Javy 的评论得出的结论:

      import java.beans.PropertyEditorSupport;
      import java.time.LocalDate;
      import java.time.LocalDateTime;
      import java.time.LocalTime;
      
      public class CustomLocalDateTimeEditor extends PropertyEditorSupport {
      
         public CustomLocalDateTimeEditor() {
         }
      
         private LocalDateTime parseText(String text) {
            LocalDateTime ldt;
            try {
               ldt = LocalDateTime.parse(text);
            } catch(Exception ee) {
               ldt = null;
            }
      
            if(ldt == null) {
               try {
                  ldt = LocalDateTime.of(LocalDate.parse(text), LocalTime.of(0, 0));
               } catch(Exception ee) {
                  ldt = null;
               }
            }
      
            return ldt;
         }
      
         @Override
         public void setAsText(String text) throws IllegalArgumentException {
            setValue(parseText(text));
         }
      
         @Override
         public String getAsText() {
            LocalDateTime value = parseText(String.valueOf(getValue()));
            return (value != null ? value.toString() : "");
         }
      
      }
      

      在 XML 中我有以下内容:

      <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
         <property name="customEditors">
            <map>
               <entry key="java.time.LocalDateTime" value="com.mycompany.CustomLocalDateTimeEditor" />
            </map>
         </property>
      </bean>
      
      <bean id="myClass" class="MyClass">
         <property name="startDate">
            <value>2000-01-01</value>
         </property>
      </bean>
      

      【讨论】:

        【解决方案3】:

        您可以使用@DateTimeFormat 注释并提供模式。例如

        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private LocalDateTime startDate;
        

        当 Spring 找到 Joda localdatetime 时,它​​会将日期转换为适当的格式。

        @DateTimeFormat in spring

        【讨论】:

        • 正如我在问题中所述 - 我无法使用此解决方案。
        • 您在 spring 配置文件中设置 MyClass 的 startDate 属性,那么您怎么可能在 spring 之外使用呢?如果你在 spring 配置文件中声明你的 bean,那么 spring 将读取 bean 并抛出任何错误,否则 spring 将永远不会读取你的 bean。
        • 包含 MyClass 的库不使用 Spring,但遵循 bean 约定。使用这个库的应用程序也使用 Spring,并且可以配置它使用的对象。
        【解决方案4】:

        声明一个dateFormat bean,在“MyClass”bean 中,引用“dateFormat”bean 作为工厂bean。工厂方法会调用SimpleDateFormat.parse()自动将String转换为Date对象。

        参考: http://www.mkyong.com/spring/spring-how-to-pass-a-date-into-bean-property-customdateeditor/

        【讨论】:

          猜你喜欢
          • 2019-04-21
          • 2017-02-15
          • 1970-01-01
          • 2017-08-07
          • 2014-12-25
          • 1970-01-01
          • 2018-10-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多