더북(TheBook)

4.7.1 스프링이 기본으로 제공하는 PropertyEditor 사용하기

다음은 프로퍼티 14개를 선언하는 간단한 빈의 코드로, 스프링에 내장된 PropertyEditor 구현체가 14개 프로퍼티 타입의 변환을 지원합니다.

예제 4-33 14개 프로퍼티를 내장 PropertyEditor로 변환([[property-editors]] PropertyEditorBean.java)

package com.apress.prospring5.ch4;

import java.io.File; import java.io.InputStream; import java.net.URL; import java.util.*; import java.util.regex.Pattern; import java.text.SimpleDateFormat; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;

import org.springframework.context.support.GenericXmlApplicationContext;

public class PropertyEditorBean { private byte[] bytes; // ByteArrayPropertyEditor private Character character; // CharacterEditor private Class cls; // ClassEditor private Boolean trueOrFalse; // CustomBooleanEditor private List<String> stringList; // CustomCollectionEditor private Date date; // CustomDateEditor private Float floatValue; // CustomNumberEditor private File file; // FileEditor private InputStream stream; // InputStreamEditor private Locale locale; // LocaleEditor private Pattern pattern; // PatternEditor private Properties properties; // PropertiesEditor private String trimString; // StringTrimmerEditor private URL url; // URLEditor

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setCharacter</span><span class="o">(</span><span class="n">Character</span> <span class="n">character</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Character 설정: "</span> <span class="o">+</span> <span class="n">character</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">character</span> <span class="o">=</span> <span class="n">character</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setCls</span><span class="o">(</span><span class="n">Class</span> <span class="n">cls</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Class 설정: "</span> <span class="o">+</span> <span class="n">cls</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
    <span class="k">this</span><span class="o">.</span><span class="na">cls</span> <span class="o">=</span> <span class="n">cls</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setFile</span><span class="o">(</span><span class="n">File</span> <span class="n">file</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"File 설정: "</span> <span class="o">+</span> <span class="n">file</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
    <span class="k">this</span><span class="o">.</span><span class="na">file</span> <span class="o">=</span> <span class="n">file</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setLocale</span><span class="o">(</span><span class="n">Locale</span> <span class="n">locale</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Locale 설정: "</span> <span class="o">+</span> <span class="n">locale</span><span class="o">.</span><span class="na">getDisplayName</span><span class="o">());</span>
    <span class="k">this</span><span class="o">.</span><span class="na">locale</span> <span class="o">=</span> <span class="n">locale</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setProperties</span><span class="o">(</span><span class="n">Properties</span> <span class="n">properties</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"읽어 들인 Properties 개수 : "</span> <span class="o">+</span> <span class="n">properties</span><span class="o">.</span><span class="na">size</span><span class="o">()</span> <span class="o">+</span> <span class="s">"개"</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">properties</span> <span class="o">=</span> <span class="n">properties</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUrl</span><span class="o">(</span><span class="n">URL</span> <span class="n">url</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"URL 설정: "</span> <span class="o">+</span> <span class="n">url</span><span class="o">.</span><span class="na">toExternalForm</span><span class="o">());</span>
    <span class="k">this</span><span class="o">.</span><span class="na">url</span> <span class="o">=</span> <span class="n">url</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setBytes</span><span class="o">(</span><span class="kt">byte</span><span class="o">...</span> <span class="n">bytes</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"bytes 설정: "</span> <span class="o">+</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">toString</span><span class="o">(</span><span class="n">bytes</span><span class="o">));</span>
    <span class="k">this</span><span class="o">.</span><span class="na">bytes</span> <span class="o">=</span> <span class="n">bytes</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setTrueOrFalse</span><span class="o">(</span><span class="n">Boolean</span> <span class="n">trueOrFalse</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Boolean 설정: "</span> <span class="o">+</span> <span class="n">trueOrFalse</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">trueOrFalse</span> <span class="o">=</span> <span class="n">trueOrFalse</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setStringList</span><span class="o">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">stringList</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"String 목록 설정. 크기: "</span>
        <span class="o">+</span> <span class="n">stringList</span><span class="o">.</span><span class="na">size</span><span class="o">());</span>

    <span class="k">this</span><span class="o">.</span><span class="na">stringList</span> <span class="o">=</span> <span class="n">stringList</span><span class="o">;</span>

    <span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">string</span><span class="o">:</span> <span class="n">stringList</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">" String 멤버: "</span> <span class="o">+</span> <span class="n">string</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setDate</span><span class="o">(</span><span class="n">Date</span> <span class="n">date</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Date 설정: "</span> <span class="o">+</span> <span class="n">date</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">date</span> <span class="o">=</span> <span class="n">date</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setFloatValue</span><span class="o">(</span><span class="n">Float</span> <span class="n">floatValue</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Float 값 설정: "</span> <span class="o">+</span> <span class="n">floatValue</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">floatValue</span> <span class="o">=</span> <span class="n">floatValue</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setStream</span><span class="o">(</span><span class="n">InputStream</span> <span class="n">stream</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Stream 설정: "</span> <span class="o">+</span> <span class="n">stream</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">stream</span> <span class="o">=</span> <span class="n">stream</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setPattern</span><span class="o">(</span><span class="n">Pattern</span> <span class="n">pattern</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Pattern 설정: "</span> <span class="o">+</span> <span class="n">pattern</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">pattern</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setTrimString</span><span class="o">(</span><span class="n">String</span> <span class="n">trimString</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"문자열 trim 설정: "</span> <span class="o">+</span> <span class="n">trimString</span><span class="o">);</span>
    <span class="k">this</span><span class="o">.</span><span class="na">trimString</span> <span class="o">=</span> <span class="n">trimString</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kd">static</span> <span class="kd">class</span> <span class="nc">CustomPropertyEditorRegistrar</span> 
        <span class="kd">implements</span> <span class="n">PropertyEditorRegistrar</span> <span class="o">{</span> 
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">registerCustomEditors</span><span class="o">(</span><span class="n">PropertyEditorRegistry</span> <span class="n">registry</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">SimpleDateFormat</span> <span class="n">dateFormatter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SimpleDateFormat</span><span class="o">(</span><span class="s">"yyyy/MM/dd"</span><span class="o">);</span>
        <span class="n">registry</span><span class="o">.</span><span class="na">registerCustomEditor</span><span class="o">(</span><span class="n">Date</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> 
            <span class="k">new</span> <span class="n">CustomDateEditor</span><span class="o">(</span><span class="n">dateFormatter</span><span class="o">,</span> <span class="kc">true</span><span class="o">));</span>

        <span class="n">registry</span><span class="o">.</span><span class="na">registerCustomEditor</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> 
            <span class="k">new</span> <span class="n">StringTrimmerEditor</span><span class="o">(</span><span class="kc">true</span><span class="o">));</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">...</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
    <span class="n">File</span> <span class="n">file</span> <span class="o">=</span> <span class="n">File</span><span class="o">.</span><span class="na">createTempFile</span><span class="o">(</span><span class="s">"test"</span><span class="o">,</span> <span class="s">"txt"</span><span class="o">);</span>
    <span class="n">file</span><span class="o">.</span><span class="na">deleteOnExit</span><span class="o">();</span>

    <span class="n">GenericXmlApplicationContext</span> <span class="n">ctx</span> <span class="o">=</span> <span class="k">new</span> <span class="n">GenericXmlApplicationContext</span><span class="o">();</span>
    <span class="n">ctx</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="s">"classpath:spring/app-context-01.xml"</span><span class="o">);</span>
    <span class="n">ctx</span><span class="o">.</span><span class="na">refresh</span><span class="o">();</span>

    <span class="n">PropertyEditorBean</span> <span class="n">bean</span> <span class="o">=</span> 
        <span class="o">(</span><span class="n">PropertyEditorBean</span><span class="o">)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">getBean</span><span class="o">(</span><span class="s">"builtInSample"</span><span class="o">);</span>

    <span class="n">ctx</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
<span class="o">}</span>

}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.