Spring Framework

날짜 데이터 Injection

최성헌 2017. 7. 6. 10:14

 - Example : DateProperty.java

import java.util.Date;

/**
* Created on 2017. 7. 6.
*
* @author Sung-Hun Choi
* @since JDK1.8
*/
public class DateProperty {

private Date date;


public void setDate(Date date) {
this.date = date;
}

public void printDate() {
System.out.println(date);
}
}


위 클레스의 date 변수에 날짜 데이터를 넣도록 설정해보겠다.


- Example : date-property.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean class="DateProperty">
<property name="date" value="2017-07-06" />
</bean>

</beans>

위와 같이 스프링 설정을 해놓고 DateProperty 클레스의 pritDate() 메소드를 실행 해본다.

@Test
public void testDateProperty() {
final String xmlPath = "spring/date-property.xml";
final ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);

final DateProperty dateProperty = context.getBean(DateProperty.class);
dateProperty.printDate();
}

그러면 아래와 같은 오류 메세지가 발생 한다.


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DateProperty#0' defined in class path resource [spring/date-property.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Date' for property 'date': no matching editors or conversion strategy found


데이터 타입이 맞지 않아서 bean 초기화에 실패 하였다는 오류이다.

이걸 해결하기 위해서는 Spring에서 제공하는 속성 편집기 중에서 CustomDateEditor 라는 놈을 스프링 컨테이너 속성 편집기에 등록을 해야 한다.

등록하는 방법은 다음과 같다.

  1. PropertyEditorRegistrar 인터페이스 구현 클레스 만들기
  2. CustomEditorConfigurer 클레스 구성

- Example : MyPropertyEditorRegistrar.java
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Created on 2017. 7. 5.
*
* @author Sung-Hun Choi
* @since JDK1.8
*/
public class MyPropertyEditorRegistrar implements PropertyEditorRegistrar {

@Override
public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)
);
}
}

- Example : date-property.xml 에 CustomEditorConfigurer 구성 추가

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean class="DateProperty">
<property name="date" value="2017-07-06" />
</bean>

<bean id="myPropertyEditorRegistrar" class="MyPropertyEditorRegistrar" />

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="myPropertyEditorRegistrar" />
</list>
</property>
</bean>

</beans>


설정을 마치고 동일한 테스트 코드를 실행하여 날짜를 확인해 보면 입력한 2017년 7월 6일로 Date 클레스 값이 설정 되어 있는 것을 확인 할 수 있다.