<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring 屬性編輯器 – `CustomEditorConfigurer`示例 > 原文: [https://howtodoinjava.com/spring-core/registering-built-in-property-editors-in-spring-4-customeditorconfigurer-example/](https://howtodoinjava.com/spring-core/registering-built-in-property-editors-in-spring-4-customeditorconfigurer-example/) 屬性編輯器是 JavaBeans API 的一項功能,用于在文本值之間來回轉換屬性值。 每個屬性編輯器僅設計用于某種類型的屬性。 您可能希望使用屬性編輯器來簡化 bean 配置。 在本教程中,我們將學習在您的應用程序中配置 spring 的內置`CustomDateEditor`類。 ## `CustomEditorConfigurer`和`CustomDateEditor`配置 通常,您將在使用容器之前在容器中注冊一個屬性編輯器。 `CustomEditorConfigurer`類被實現為內置的 bean 工廠后處理器,供您在實例化任何 bean 之前注冊自定義屬性編輯器。 #### 為什么選擇`CustomDateEditor` 例如,在您的應用程序中,如果要將日期值從字符串格式轉換為`java.util.Date`對象,反之亦然,則可以使用`CustomDateEditor`類。 Spring 附帶的`CustomDateEditor`類用于將日期字符串轉換為`java.util.Date`屬性。 可以將`CustomEditorConfigurer` bean 聲明到應用程序上下文中,如下所示: `beans.xml` ```java <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <bean class="com.howtodoinjava.demo.processors.CustomDateEditorRegistrar" /> </list> </property> </bean> ``` #### 注冊/配置`CustomDateEditor` 從 **spring 4.x 開始**應該以以下方式聲明`CustomDateEditorRegistrar`類。 `CustomDateEditorRegistrar.java` ```java public class CustomDateEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)); } } ``` ## `CustomDateEditor`演示 現在,每次,當您以字符串格式(例如)傳遞 bean 屬性值(類型`java.util.Date`)時, `2007-09-30`,它將自動轉換為`Date`對象。 讓我們測試配置。 為了測試,我創建了一個`EmployeeDTO` bean,其中一個日期字段為`dateOfBirth`。 ```java public class EmployeeDTO { private Integer id; private String firstName; private String lastName; private String designation; private Date dateOfBirth; //Setters and Getters @Override public String toString() { return "EmployeeDTO [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", designation=" + designation + ", dateOfBirth=" + dateOfBirth + "]"; } } ``` `applicationContext.xml`文件中的`Employee` bean 定義如下: `applicationContext.xml` ```java <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <bean class="com.howtodoinjava.demo.processors.CustomDateEditorRegistrar" /> </list> </property> </bean> <!-- employeeDTO bean --> <bean id="employeeDTO" class="com.howtodoinjava.demo.model.EmployeeDTO"> <property name="firstName" value="Lokesh" /> <property name="lastName" value="Gupta" /> <property name="designation" value="Manager" /> <property name="dateOfBirth" value="2007-09-30" /> </bean> ``` 讓我們從上下文中獲取 bean。 應該在`dateOfBirth`字段中填充給定的日期值。 `TestSpringContext.java` ```java public class TestSpringContext { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); EmployeeDTO employeeDTO = (EmployeeDTO) context.getBean("employeeDTO"); System.out.println(employeeDTO.getDateOfBirth()); } } Output: Sun Sep 30 00:00:00 IST 2007 ``` 很棒。`Date`值被設置。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看